Removing fragments from FragmentStatePagerAdapter

前端 未结 3 985
轻奢々
轻奢々 2020-12-05 20:59

UPDATE

After some major fighting with this problem and help from SO users I managed to solve it. This is how my clearProgressUpTo looks

3条回答
  •  暖寄归人
    2020-12-05 21:26

    When you call notifyDataSetChanged() the adapter starts removing its fragments using destroyItem(ViewGroup container, int position, Object object).

    However, each fragment's instance state is retained internally by FragmentStatePagerAdapter to be later used in instantiateItem(ViewGroup container, int position).

    Simply save item's position you want to remove and call notifyDataSetChanged(). Just after the last item is destroyed, retrieve the desired saved instance state from FragmentStatePagerAdapter and replace with null.

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    
        if(position == getCount() - 1 && i >= 0){
            Bundle state = (Bundle) saveState();
            Fragment.SavedState[] states = (Fragment.SavedState[]) state.getParcelableArray("states");
            if (states != null)
                states[i] = null;
            i = -1;
            restoreState(state, ClassLoader.getSystemClassLoader());
        }
    

    Note: getItemPosition(Object object) must return PagerAdapter.POSITION_NONE.

提交回复
热议问题