0

I'm using ViewPager to show list of Posts and its adapter extends FragmentStatePagerAdapter . The problem that i'm facing is that when any post is deleted then i'm calling following method to remove the item from the list. But the view is not updated. When i scroll the list and later on if i scroll it back then that item is removed from the list. Using 'POSITION_NONE' in getPositionItem makes my screen blank white.

Method in PagerAdapter:

       public class TestFragmentAdapter extends FragmentStatePagerAdapter  {
        Context mContext;
        LayoutInflater mLayoutInflater;
        private ArrayList<PostsDataItem> postList;

        public HomeFragmentAdapter(Context context, FragmentManager fm) {
            super(fm);
            this.mContext = context;
            this.postList = new ArrayList<>();
            mLayoutInflater = LayoutInflater.from(mContext);

        }

        @Override
        public Fragment getItem(int position) {
            return PostFragment.newInstance(postList.get(position), position);
        }

        public void removeItem(int position) {
            postList.remove(position);
            notifyDataSetChanged();
        }
/*
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }*/

    }
Usman Rana
  • 1,429
  • 1
  • 13
  • 30

2 Answers2

1

You should use this code inside your adapter class for pager refresh items.

// This is called when notifyDataSetChanged() is called
@Override
public int getItemPosition(Object object) {
    // refresh all fragments when data set changed
    return PagerAdapter.POSITION_NONE;
}
mbayram93
  • 51
  • 3
0

Change your getItemPosition as below and check if it doesn't causing any problem:-

@Override
public int getItemPosition(Object object) {
    PostFragment fragment = (PostFragment)object;
        String title = fragment.getTitle();
        int position = titles.indexOf(title);

        if (position >= 0) {
            return position;
        } else {
            return PagerAdapter.POSITION_NONE;
        }

}
Nainal
  • 1,618
  • 11
  • 23