If you want to remove items from the ViewPager , this following code does not make sense:
@Override public Fragment getItem(int position) { return CreateWishFormDatePaginationFragment.newInstance(position); }
Essentially, you create a Fragment based on position . No matter which page you delete, the range of position will change from [0, iPageCount ) to [0, iPageCount-1 ) , which means that it will always be rid of the last Fragment .
What you need is more or less the following:
public class DatePickerPagerAdapter extends FragmentStatePagerAdapter { private ArrayList<Integer> pageIndexes; public DatePickerPagerAdapter(FragmentManager fm) { super(fm); pageIndexes = new ArrayList<>(); for (int i = 0; i < 10; i++) { pageIndexes.add(new Integer(i)); } } @Override public int getCount() { return pageIndexes.size(); } @Override public Fragment getItem(int position) { Integer index = pageIndexes.get(position); return CreateWishFormDatePaginationFragment.newInstance(index.intValue()); }
For more information on removing an item from the FragmentStatePagerAdapter, see the full example .
source share