I've been working on a TAB layout with 3 tabs.
My problem is I'm unable to update,refresh 2nd tab swiping between the pages.
As per my debugging I swipe from tab one to second, the third is being called at getItem(int position), similarly fragment one is being called swiping from third tab to second.
The default getOffscreenPageLimit of viewpager is 1. I couldn't even set it to zero count.
At getItem, position is 1 at the very beginning but as soon as I swipe page, it only calls position 0 and 2. 1 is never called.
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
This is my ViewPagerSetUp
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new
ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new InvitedFragment(), "Invited");
adapter.addFragment(new InterestedFragment(), "Interested");
adapter.addFragment(new AttendingFragment(), "Attending");
viewPager.setAdapter(adapter);
}
This is my ViewPager
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void refreshFragment(int position) {
mFragmentList.get(position).onResume();
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
addOnPageChangeListener was one option but I couldn't refresh it using pageChangelistener too.
How can I cure offscreenpagelimit ? How can I refresh every tab on every page swipe?
Thank you.
OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.
ViewPager is a layout manager that allows the user to flip left and right through pages of data. It is mostly found in apps like Youtube, Snapchat where the user shifts right – left to switch to a screen. Instead of using activities fragments are used.
You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .
Try this,
Override the below method in fragment.It call when fragment is visible
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//do your Stuffs
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With