We\'re suffering from a very strange issue with ViewPager here. We embed lists on each ViewPager page, and trigger notifyDataSetChanged both on the list adapter and the view
We finally managed to find a solution. Apparently our implementation suffered of two issues:
destroyItem()
.destroyItem()
, we were not adding it in instantiateItem()
but just returning the cached view corresponding to the current position.I haven't looked too deeply in the source code of the ViewPager
- and it's not exactly explicit that you have to do that - but the docs says :
destroyItem()
Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).
and:
A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int) after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.
So my conclusion is that ViewPager
relies on its underlying adapter to explicitly add/remove its children in instantiateItem()
/destroyItem()
. That is, if your adapter is a subclass of PagerAdapter
, your subclass must implement this logic.
Side note: be aware of this if you use lists inside ViewPager
.