I'm using a ViewPager and displaying a lot of different Fragments inside it, not only in content but they use different classes as well. The list to be displayed should be changed dynamically and even though I manage to swap items around and add new ones to the adapter(and calling notifyDataSetChanged), if I try changing the next item it will still slide to it when using mPager.setCurrentItem(mPager.getCurrentItem() + 1);
I am just adding a new Fragment between the current item and the current next one, it is displayed correctly in the adapter but as the next one was already preloaded then getItem in the adapter is not even called.
Is there another method "stronger" than notifyDataSetChanged that tells my ViewPager that it should get the next item again?
CODE SAMPLES:
The add and get item methods inside my FragmentPagerAdapter(only samples, not the actual code)
public void add(@NonNull Integer fragmentIndex) { mFragmentOrder.add(fragmentIndex); notifyDataSetChanged(); } @Override public Fragment getItem(int position) { int selectedFragment = mFragmentOrder(position); Fragment fragment; switch (selectedFragment) { case 1: fragment = new FragmentA(); break; case 2: fragment = new FragmentB(); break; case 3: fragment = new FragmentC(); break; default: fragment = new FragmentD(); break; } return fragment; } This is the function used to go to the next item(I don't allow swiping)
public void goToNext() { mPager.setCurrentItem(mPager.getCurrentItem() + 1); } EDITS:
Edit 1: I had already tried using a FragmentStatePagerAdapter instead and setting the OffscreenPageLimit to 0, but to no avail.
Edit 2: [Solution] Using a FragmentStatePagerAdapter AND overwriting the getItemPosition function to return POSITION_NONE or the index in the appropriate cases solved the problem. For some reason even after implementing the right version of this function the normal FragmentPagerAdapter kept delivering the wrong Fragment.
FragmentStateAdapteras well, it wouldn't fix the problem anyway. As I said, theViewPageris not requerying the adapter for the new "next" slide, it just shows the one already preloaded. Trying to set the off page limit does nothing as well.