Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewpager and FragmentPagerAdapter: Page view removal

I'm using a Viewpager with a the FragmentPagerAdapter to allow adding and removing of pages. Each page displays data obtained from the internet.

As a new page is added, a new Fragment is associated with that page. The data are obtained via AsyncTask and displayed in the Fragment. When the user chooses to remove a page, the idea is to destroy the page and the associated Fragment.

In general, this all works well. The problem I'm seeing is as follows:

  1. You have three pages with data:

    [Page 1] [Page 2] [Page 3]

  2. You delete any page other than the last one, say page 2; Page 2 disappears as desired:

    [Page 1] [Page 3]

  3. You add a new page; but instead of a blank, new page, the new page shows the data (view) from page 3.

    [Page 1] [Page 3] [Page 4, but showing view/data of Page 3, should be blank]


The page removal code in my activity is as follows:

   // Destroy fragment for this page
   DataListFragment curFrag = getFragmentByName(currentPage);
   FragmentManager fm = getSupportFragmentManager();
   fm.beginTransaction().remove(curFrag).commit();
   fm.executePendingTransactions();
   curFrag = null;

   // Remove page and update adapter
   mPageTitles.remove(position);        
   mAdapter.notifyDataSetChanged();

Using the debugger, it shows that the fragment is removed from the FragmentManager after the executePendingTransactions() call. But in the FrampePagerAdapters call, mAdapter.notifyDataSetChanged(), the fragment is added back in and then displayed when a new page is created.

I tried using the FrameStatePagerAdapter, since that should allow destroying fragments, but it did not work. In my FragmentPagerAdapter's getItemPosition() method, I use return FragmentAdapter.POSITION_NONE; as pointed out in another SO article I came across.

It seems as if the View for that page is not destroyed, but then added back into the new page. I tried using the removeViewAt() method on the view of the new page, but that did not work.


Being new to this, I'm sure I'm missing something obvious...

like image 872
mraviator Avatar asked Sep 07 '12 09:09

mraviator


People also ask

Is ViewPager deprecated?

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated.

Can I use ViewPager with views not with fragments?

In ViewPager2 you do not need to use Fragments at all. Just create an Adapter as you would a RecyclerView Adapter. and set it to ViewPager2. One thing to keep in mind however is layout height of the view you want to inflate should be match_parent and not wrap_content as you would for a recyclerView.

How do I remove items from ViewPager Android?

The getItemPosition() method tells the ViewPager whether or not to delete viewItem, to redraw or not redraw viewItem when the adapter. notifyDataSetChanged() is called. Basically your implementation of getItemPosition() method is called to answer a simple question when adapter. notifyDataSetChanged() is called.

What is the difference between PagerAdapter and FragmentPagerAdapter?

The difference is that you can use Fragments inside a FragmentPageAdapter . If you want to have fragments that are going to be used in other parts of your code this is your Adapter. Otherwise if you only want to have code that isn't going to be reused, you can use PagerAdapter .


1 Answers

You should rather extend FragmentStatePagerAdapter and remove corresponding item from the list of items in that adapter, instead of trying to remove a Fragment from activity. Do not forget to call adapter.notifyDataSetChanged() after you removed an item in adapter. Once done, ViewPager and FragmentStatePagerAdapter will take care for the rest.

like image 171
sergej shafarenka Avatar answered Nov 15 '22 19:11

sergej shafarenka