My Activity
contains a ViewPager
and it's custom Adapter
which extends FragmentStatePagerAdapter
. ViewPager
contains 3 fragments
Code to remove Fragments from the ViewPager
MainActivity
public void deleteElement(){
final int currentItem = mViewPager.getCurrentItem();
mPagerAdapter.removeItem(currentItem);
mPagerAdapter.notifyDataSetChanged();
}
CustomPagerAdapter
private ArrayList<Item> data;
public void removeItem(int index){
data.remove(index);
}
when removing the middle Fragment
(index 1):
- from
data
i'm removing the correctitem
. - Problem is that i (i guess)
Fragment
3 is removed afternotifyDataSetChanged
and the currentFragment
is still the fragment that the user saw and the data that is being loaded is from theSavedInstance
bundle
So the end result that the user still see the same Fragment
that he tried to remove which is kinda suck for him.
Ideas?
***** EDIT ******
seems like ViewPager2 Features might solve this issue and many other issues as well
ArrayPagerAdapter
, which handles this. I am not terribly surprised thatFragmentStatePagerAdapter
has problems in this area, which is why I rolled something separate. – Gamaliel