I am not experienced on android and I implemented code for Tablayout, where I have six tab and each tab has there ListView where data will come from server. I am using VolleyResponse to fetch the data but all the tab's data come at open time. I need data should be come on swipe and when user will go previous swipe data should not load again. So please guide me what should I do?
Below is my code for fragment.
private void setupViewPager(ViewPager viewPager) {
int numberOfPages=7;
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Matches_Tab(), "Matches");
adapter.addFragment(new NewMatches_Tab(), "New matches");
adapter.addFragment(new Similar_Matchs_Tab(), "Similar Matches");
adapter.addFragment(new ShortlistTab(), "ShortListed");
adapter.addFragment(new Viewed_My_Profile(), "View My Profile");
adapter.addFragment(new ShortListedMeTab(), "ShortListed Me");
adapter.addFragment(new Photo_Request_Received(), "Photo Request Received");
viewPager.setOffscreenPageLimit(numberOfPages);
viewPager.setAdapter(adapter);
}
Fragment class
public class Matches_Tab extends Fragment{
// Session Manager Class
SessionManager session;
String email;
public String JSON_URL;
private ListView listView;
public Matches_Tab() {}
private static Matches_Tab instance;
public static synchronized Matches_Tab getInstance(Bundle data){
if (instance == null) {
instance = new Matches_Tab();
instance.setArguments(data);
}
return instance;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TypefaceUtil.overrideFont(getContext(), "SERIF", "Peddana-Regular.ttf");
// Session class instance
session = new SessionManager(getActivity());
// get user data from session
HashMap<String, String> user = session.getUserDetails();
email = user.get(SessionManager.KEY_EMAIL);
JSON_URL = "https://www.maangal.com/maangal_mobile/matches.php?matri_id="+email;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.matches_tab, container, false);
// ProgressBar progressbar = (ProgressBar) view.findViewById(R.id.progressBar);
listView = (ListView) view.findViewById(R.id.listView);
sendRequest();
//getArguments();
return view;
}
private void sendRequest(){
final ProgressDialog loading = ProgressDialog.show(getActivity(),"Loading Data", "Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.POST,JSON_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
Log.e("Broder Matches s----->",response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
});
int MY_SOCKET_TIMEOUT_MS = 30000;
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
protected void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
Profile_Match_custom_List cl = new Profile_Match_custom_List(getActivity(), ParseJSON.ids,ParseJSON.ages, ParseJSON.heights, ParseJSON.communities,ParseJSON.castes,ParseJSON.educations,ParseJSON.occupations,ParseJSON.incomes,ParseJSON.pics,ParseJSON.locations,ParseJSON.shortlist,ParseJSON.expressinterest);
listView.setAdapter(cl);
}
}
You can extend BaseFragment
public abstract class BaseFragment extends Fragment {
protected Activity mActivity;
/**
* isFragmentVisible
*/
private boolean isFragmentVisible;
/**
* View in onCreateView is ok
*/
private boolean isPrepared;
/**
* isFirstLoad
*/
private boolean isFirstLoad = true;
/**
* isForceLoad
*/
private boolean isForceLoad = false;
/**
* onAttach Activity
*
* #param context
*/
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.mActivity = (Activity) context;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container
, Bundle savedInstanceState) {
isFirstLoad = true;
View view = LayoutInflater.from(mActivity)
.inflate(getLayoutId(), container, false);
initView(view, savedInstanceState);
isPrepared = true;
lazyLoad();
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onDestroy() {
super.onDestroy();
}
/**
* want to load data ,need set in onCreateView
* isPrepared = true;
*/
protected void lazyLoad() {
if (isPrepared() && isFragmentVisible()) {
if (isForceLoad || isFirstLoad()) {
isForceLoad = false;
isFirstLoad = false;
initData();
}
}
}
public boolean isPrepared() {
return isPrepared;
}
public boolean isFirstLoad() {
return isFirstLoad;
}
public boolean isFragmentVisible() {
return isFragmentVisible;
}
/**
* use with viewpager
*
* #param isVisibleToUser is visible for user
*/
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getUserVisibleHint()) {
onVisible();
} else {
onInvisible();
}
}
/**
* if you use show hide to set fragment ,do this
*
* #param hidden hidden True if the fragment is now hidden, false if it is not
* visible.
*/
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (!hidden) {
onVisible();
} else {
onInvisible();
}
}
protected void onVisible() {
isFragmentVisible = true;
lazyLoad();
}
protected void onInvisible() {
isFragmentVisible = false;
}
/**
* set force load
*/
public void setForceLoad(boolean forceLoad) {
this.isForceLoad = forceLoad;
}
/**
* getLayoutId ,like : R.layout.fragment
*
* #return
*/
protected abstract int getLayoutId();
/**
* initView
*
* #param view
* #param savedInstanceState
*/
protected abstract void initView(View view, Bundle savedInstanceState);
/**
* load data here
*/
protected abstract void initData();
}
Then load data in initData method .
#Override
protected void initData() {
// request here
}
Note
the main method is setUserVisibleHint and onHiddenChanged
add some boolean flag to determine whether the data has been loaded, view creates etc.
You can use ViewPager.OnPageChangeListener to get callbacks for when a page becomes visible.
Check below code.
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// Add your logic
}
});
Aternate solution
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
//put your Logic here
}
});
If you are using ViewPager and you are getting data for all the tabs in one API call then I suggest you set the offset of the viewpager equal to the number of tabs.
ViewPager vp = ...;
vp.setOffscreenPageLimit(numberOfTabsHERE);
NOTE: Only use this if there is not much data to show else you may get a MemoryOutOfException.
If you do have a lot of tabs and a lot of data. Using Singleton pattern for fragments is a good option.
public class MyFragment extends Fragment {
private static MyFragment instance;
public static synchronized MyFragment getInstance(Bundle data // if any){
if (instance == null) {
instance = new MyFragment();
instance.setArguments(data);
}
return instance;
}
}
...
onCreateView{.... getArguments()}
Related
In my application , I have ViewPager containing 3 fragments (Home , Profile and More)
Home Fragment contain RecyclerView
when i navigate from Home Fragment to Profile Fragment or More Fragment, it work like expected.
But when i navigate back to Home Fragment , items in RecyclerView duplicate...
And when i navigate again and back , it duplicate again
Here is my MainActivity.java Code:
public class MainActivity extends AppCompatActivity {
TabLayout mainTabLayout;
ViewPager mainViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainTabLayout = findViewById(R.id.main_tablayout);
mainViewPager = findViewById(R.id.main_viewpager);
mainTabLayout.setupWithViewPager(mainViewPager);
//to put mainTabLayout Icons...
MainTabLayoutIcons();
}
//to Change MainLayout Icons...
private void MainTabLayoutIcons() {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
// add fragments to the adapter
adapter.addFragment(new Home());
adapter.addFragment(new MyActivity());
adapter.addFragment(new More());
mainViewPager.setOffscreenPageLimit(0);
mainViewPager.setAdapter(adapter);
// Select Home tab as default tab on startup
mainViewPager.setCurrentItem(0);
mainTabLayout.getTabAt(0).select();
// Set tabs Initial icons
mainTabLayout.getTabAt(0).setIcon(R.drawable.home_black);
mainTabLayout.getTabAt(1).setIcon(R.drawable.user_outline);
mainTabLayout.getTabAt(2).setIcon(R.drawable.more_outline);
/* tabSelectedListener to change the icon color once it is selected
// and change it back once another tab is selected */
mainTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 0) {
mainTabLayout.getTabAt(0).setIcon(R.drawable.home_black);
mainTabLayout.getTabAt(1).setIcon(R.drawable.user_outline);
mainTabLayout.getTabAt(2).setIcon(R.drawable.more_outline);
}
if (tab.getPosition() == 1) {
mainTabLayout.getTabAt(0).setIcon(R.drawable.home_outline);
mainTabLayout.getTabAt(1).setIcon(R.drawable.user_);
mainTabLayout.getTabAt(2).setIcon(R.drawable.more_outline);
}
if (tab.getPosition() == 2) {
mainTabLayout.getTabAt(0).setIcon(R.drawable.home_outline);
mainTabLayout.getTabAt(1).setIcon(R.drawable.user_outline);
mainTabLayout.getTabAt(2).setIcon(R.drawable.more_black);
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
//no need for it
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
//no need for it
}
});
}
}
and here is ViewPagerAdapter Code:
//Adapter for mainActivity ...
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
public ViewPagerAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
public void addFragment(Fragment fragment) {
fragmentList.add(fragment);
}
}
and here is first Fragment Home.java Code:
public class Home extends Fragment {
View v;
public SwipeRefreshLayout mSwipeRefreshLayout;
public RecyclerView mainRecyclerView;
private LinearLayoutManager linearLayoutManager;
public final List<MyActivityModel> mainPosts = new ArrayList<>();
private PostAdapter mAdapter;
public Home() {
/*Required Empty Constructor... */
}
#Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle bundle) {
v = layoutInflater.inflate(R.layout.fragment_home, container, false);
mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.mainSwipeRefresh);
mainRecyclerView = (RecyclerView) v.findViewById(R.id.MainRecyclerView);
linearLayoutManager = new LinearLayoutManager(getActivity());
mainRecyclerView.setLayoutFrozen(true);
mainRecyclerView.setNestedScrollingEnabled(false);
mainRecyclerView.setLayoutManager(linearLayoutManager);
mAdapter = new PostAdapter(mainPosts);
mainRecyclerView.setAdapter(mAdapter);
Swipe();
LoadPosts();
return v;
}
/**
* -----------------TODO: to load posts in Home RecyclerView...----------------
*/
public void LoadPosts() {
Query database = FirebaseDatabase.getInstance().getReference().child("Posts");
database.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
if (snapshot.exists()) {
MyActivityModel post = snapshot.getValue(MyActivityModel.class);
mainPosts.add(0, post);
mAdapter.notifyDataSetChanged();
}
}
#Override
public void onChildChanged(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot snapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
private void Swipe() {
//to refresh page after swipe
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
try {
mainPosts.clear();
LoadPosts();
mSwipeRefreshLayout.setRefreshing(false);
Toast.makeText(v.getContext(),"Refreshed", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
}, 500);
}
});
}
}
and this is an image showing item reeating
Anyone can help me how to prevent this duplication...
You may need to use FragmentPagerAdapter instead of FragmentStatePagerAdapter. Also, set mainRecyclerView.setLayoutFrozen(true); to false because, it will stop everything in recyclerView.
When you need to use FragmentPagerAdapter?
FragmentStatePagerAdapter is more useful when there are a large number
of pages, working more like a list view. When pages are not visible to
the user, their entire fragment may be destroyed, only keeping the
saved state of that fragment. This allows the pager to hold on to
much less memory associated with each visited page as compared to
FragmentPagerAdapter at the cost of potentially more overhead when
switching between pages.
Example of FragmentPagerAdapter:
class viewpager_class extends FragmentPagerAdapter {
int tabcount;
public viewpager_class(#NonNull FragmentManager fm, int tab_count) {
super(fm, tab_count);
this.tabcount = tab_count;
}
#NonNull
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment1 fragment1 = new Fragment1 ();
return todayBAFragment;
case 1:
Fragment2 fragment2 = new Fragment2 ();
return todayReminderDue;
case 2:
Fragment3 fragment3 = new Fragment3 ();
return currentTransaction;
default:
return null;
}
}
#Override
public int getCount() {
return tabcount;
}
}
Also, set adapter into ViewPager wherever you want show viewPager:
//set adapter into viewpager
viewpager_class viewpagerClass = new viewpager_class(getChildFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(viewpagerClass);
viewPager.setOffscreenPageLimit(2);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
I have a main fragment and inside the main fragment, I have a ViewPager that consists of Nested Fragment (Detail Fragment & Summary Fragment). If I click the button in the main fragment, the data will be set in both the main fragment and Detail fragments in the ViewPager. The problem is how do I get the data from the main fragments that can be used in Detail fragments?
I tried using ViewModel in the main fragment. Suppose that the data should be passing in Detail Fragment.
//Main Fragment
private void initializeViewPager()
{
vpExpenses.setOffscreenPageLimit(2);
setupViewPager(vpExpenses);
}
private void setupViewPager(final ViewPager viewPager)
{
adapter = new ViewPagerWithTitleAdapter(getFragmentManager());
adapter.addFragment(new ExpensesDetailsListFragment(), "Detail");
adapter.addFragment(new ExpensesSummaryListFragment(), "Summary");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
#Override public void onPageScrollStateChanged(int state) { }
#Override public void onPageSelected(int position) {
if(position == 0)
{
tvDetailExpenses.setSelected(true);
tvSummaryExpenses.setSelected(false);
}
else if(position == 1)
{
tvDetailExpenses.setSelected(false);
tvSummaryExpenses.setSelected(true);
}
}
});
tvDetailExpenses.setSelected(true);
viewPager.setCurrentItem(0);
tvDetailExpenses.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
viewPager.setCurrentItem(0,false);
}
});
tvSummaryExpenses.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewPager.setCurrentItem(1,false);
}
});
}
//Expenses Details List
private void setupExpensesDetailsList(String expenseCategoryId)
{
expensesViewModel = ViewModelProviders.of(this.getActivity()).get(ExpensesViewModel.class);
expensesViewModel.setToken(token);
expensesViewModel.setUserId(userId);
expensesViewModel.setDeviceId("ABCDE1234567890");
expensesViewModel.setCarId("24");
expensesViewModel.setExpensesId(expenseCategoryId);
getExpensesDetailsList();
}
public void getExpensesDetailsList()
{
expensesViewModel.getExpensesDetailsList().observe(this, expensesdetailslistObserver);
}
public Observer<ExpensesDetailsListResult> expensesdetailslistObserver = new Observer<ExpensesDetailsListResult>() {
#Override
public void onChanged(#Nullable ExpensesDetailsListResult result)
{
if(result.getStatus())
{
summary = result.getData().getSummary();
tvTotalExpenses.setText("RM"+summary.getYearly());
tvExpensesPerDay.setText("/ "+summary.getDaily_avg()+" per day");
}
else
{
if(!result.getStatus())
{
if(result.getCode() == 300)
{
new MaterialDialog.Builder(getActivity())
.title("Failed")
.content(result.getMsg())
.positiveText("OK")
.show();
}
else if(result.getCode() == 302)
{
new MaterialDialog.Builder(getActivity())
.title("Failed")
.content(result.getMsg())
.positiveText("OK")
.show();
}
}
if(result.getRedirectLogin() != null)
{
if (result.getRedirectLogin())
{
func.showErrorExpired();
}
}
}
}
};
//Details Fragment
private ExpensesDetailsListResult expensesDetails;
private List<ExpensesTypes> listExpensesType;
private Unbinder unbinder;
private ExpensesViewModel expensesViewModel;
private Function func;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_expenses_list_details, container, false);
unbinder = ButterKnife.bind(this, view);
func = new Function(getActivity());
expensesViewModel = ViewModelProviders.of(this.getActivity()).get(ExpensesViewModel.class);
expensesViewModel.getExpensesDetailsList().observe(this, item -> {
//expensesDetails = expensesViewModel.setExpensesDetailsList(item);
});
return view;
}
//View Model
public ExpensesViewModel(#NonNull Application application)
{
super(application);
}
public void setToken(String token)
{
this.token = token;
}
public void setUserId(String userId)
{
this.userId = userId;
}
public void setDeviceId(String deviceId)
{
this.deviceId = deviceId;
}
public void setCarId(String carId)
{
this.carId = carId;
}
public void setExpensesId(String expensesId)
{
this.expensesId = expensesId;
}
//THIS IS THE MAIN FUNCTION THAT UI CONTROLLER CAN OBSERVE FOR CHANGES
//Expenses Category
public MutableLiveData<ExpensesCategoryResult> getExpensesCategory()
{
expensescategoryObservable = ExpensesRepository.getInstance().getExpensesCategory(token, userId, deviceId);
return expensescategoryObservable;
}
//Expenses Details List
public MutableLiveData<ExpensesDetailsListResult> getExpensesDetailsList()
{
expensesdetailslistObservable = ExpensesRepository.getInstance().getExpensesDetailsList(token, userId, deviceId, carId, expensesId, "1");
return expensesdetailslistObservable;
}
You're passing in the wrong Lifecycle Owner when you're observing your ViewModel's data. In order for both fragments to share the data through the ViewModel, the Lifecycle Owner should be the Activity.
Both Fragments should contain something like the following:
// Get the View Model with the Activity as the owner
expensesViewModel = ViewModelProviders.of(getActivity()).get(ExpensesViewModel.class);
// Observe the LiveData by passing in the Activity as the owner, & the observer
expensesViewModel.getExpensesDetailsList().observe(getActivity(), new Observer<ExpensesDetailsListResult>(){
#Override
public void onChanged(ExpensesDetailsListResult result) {
// Do something with the data according to which Fragment this is in
}
});
I got a problem with updating ViewPager fragments. We need to show fragments with data to registered user, when he doesn't registered we need to show fragments with message to register. I use this method to check it in MainActivity:
#Override
public void setAdapter(boolean isUserExist) {
Log.d("RegDebug", "In setAdapter");
mainPagerAdapter.clearData();
mainPagerAdapter.addFragment(searchFragment, getString(R.string.search_title));
if (isUserExist) {
Log.d("RegDebug", "In setAdapter reg");
mainPagerAdapter.addFragment(new ChatsFragment(), getString(R.string.chats_title));
mainPagerAdapter.addFragment(new ActionsFragment(), getString(R.string.actions_title));
Toast.makeText(getApplicationContext(), "Registered!", Toast.LENGTH_SHORT).show();
} else {
Log.d("RegDebug", "In setAdapter unreg");
mainPagerAdapter.addFragment(RegisterFragment.newInstance(Consts.CHATS_TAB_NAME), getString(R.string.chats_title));
mainPagerAdapter.addFragment(RegisterFragment.newInstance(Consts.ACTIONS_TAB_NAME), getString(R.string.actions_title));
Toast.makeText(getApplicationContext(), "Unregistered!!!", Toast.LENGTH_SHORT).show();
}
mainPagerAdapter.notifyDataSetChanged();
viewPager.setAdapter(mainPagerAdapter);
}
I call this method in presenter with setting value from firebase auth, checking if user exists:
public void checkForUserExist() {
if (mainInteractor.isUserExist()) {
getViewState().setRegAdapter();
} else getViewState().setUnregAdapter();
}
And then call presenter method in onCreate of MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialogFragment = new FilterDialogFragment();
searchFragment = new SearchFragment();
//UI
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.main_view_pager);
tabLayout = findViewById(R.id.main_tabs);
//mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
mainPresenter.checkForUserExist();
tabLayout.setupWithViewPager(viewPager);
}
I try to log the boolean result and it returns exactly value that must be, but pager adapter can't update its content.Code of MainPagerAdapter:
public class MainPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentTitleList.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
public void addFragment(Fragment fragment, String title){
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
public void clearData(){
fragmentList.clear();
fragmentTitleList.clear();
notifyDataSetChanged();
Log.d("RegDebug", " fragmentList size is " + fragmentList.size()
+ " fragmentTitleList size is " + fragmentTitleList.size());
}
}
Use OnPageChangeListener of ViewPager class & notify to your current fragment from there using interface.
I have an activity who load a list of data, this activity its a TabActivity with 3 Tabs, all tabs load a diferent data type.
But the data may be really a bunch of data, for that reason my activity may take a lot of time to show itself.
I start teh activity using
Intent intent = new Intent(MainActivity.this, PcapAnalisys.class);
startActivity(intent);
Then, when the activity is loading shows a black screen, and after a few seconds show the content properly.
There is a way on how to show a message like: "Loading activity... please wait" or something?
This is my code for a my TabActivity:
public class PcapAnalysis extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pcap_analysis);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mViewPager = (ViewPager) findViewById(R.id.container);
tabLayout = (TabLayout) findViewById(R.id.tabs);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//fab.setImageResource(R.drawable.ic_attach_file_white_24dp);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FileChooserDialog dialog = new FileChooserDialog(PcapAnalysis.this);
dialog.show();
dialog.addListener(new FileChooserDialog.OnFileSelectedListener() {
#Override
public void onFileSelected(Dialog source, File file) {
source.hide();
Toast.makeText(source.getContext(),
"File selected: " + file.getAbsolutePath(),
Toast.LENGTH_LONG).show();
Log.i("Archivo seleccionado", file.getAbsolutePath());
new AsyncTask_load(PcapAnalysis.this, file.getAbsolutePath()).execute();
}
public void onFileSelected(Dialog source, File folder, String name) {
source.hide();
Toast.makeText(source.getContext(),
"File created: " + folder.getName() + "/" + name,
Toast.LENGTH_LONG).show();
}
});
}
});
new Task().execute();
}
#Override
public void recreate()
{
super.recreate();
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pcap_analysis, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pcap_analysis, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}*/
private class Task extends AsyncTask<Void, Integer, Void> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
pDialog = new ProgressDialog(PcapAnalysis.this);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage("Procesando...");
pDialog.setCancelable(true);
pDialog.setMax(100);
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... voids) {
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager.setOffscreenPageLimit(3); //cuantas paginas se precargan
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout.setupWithViewPager(mViewPager);
//publishProgress(progress);
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
}
#Override
protected void onPostExecute(Void result) {
pDialog.dismiss();
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private Fragment http = new HttpList();
private Fragment dns = new DnsList();
private Fragment ip = new IpList();
private Fragment resume = new ResumeList();
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
//return PlaceholderFragment.newInstance(position + 1);
switch (position) {
case 0:
return http;
case 1:
return dns;
case 2:
return ip;
case 3:
return resume;
}
return null;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "HTTP";
case 1:
return "DNS";
case 2:
return "RESOURCE IP";
case 3:
return "RESUME";
}
return null;
}
}
}
As you can see this activity use fragments to show the data, and each fragment has a for cicle to load the data (that is in a String array).
Also I use a AsyncTask trying to solve this problem, but is not working. Please help!
I am using Johan Nilsson Android Pull to Refresh library in my android application. The problem I am having is that the onRefresh method seems to not being called (since none of my logs are displayed) what is causing this and how do I fix it.
public class MyFragment extends RoboFragment implements ResponseListener<List<TimelineItem>>{
#InjectView (R.id.TwitterList) private PullToRefreshListView listView;
private List<TimelineItem> results;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_timeline, container,false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
listView.setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
Log.v("onRefresh", "done");
new FetchNewData().execute("");
}
});
TimelineTask request = new TimelineTask(getActivity());
request.onResponseListener(this);
request.execute(new String());
}
/**
* Callback handler that is called
* after data is fetched from server.
* #author mario
*/
public void onComplete(List<TimelineItem> result)
{
if (result != null && !result.isEmpty())
{
this.results = result;
Collections.sort(results,Collections.reverseOrder());
TimelineAdapter adapter = new TimelineAdapter(getActivity(),R.layout.fragment_timeline_item, results);
listView.setAdapter(adapter);
}
else
Toast.makeText(getActivity(), "No Account! Available Please an Account", Toast.LENGTH_LONG).show();
}//end onComplete method
private class FetchNewData extends AsyncTask<String, Void,List<TimelineItem> >
{
#Override
protected void onPostExecute(List<TimelineItem> result)
{
results.addAll(result);
listView.onRefreshComplete();
Log.v("onPostExecuted", "done");
}
#Override
protected List<TimelineItem> doInBackground(String... params)
{
Log.v("doInBackground", "done");
Collections.sort(results);
return results;
}
}