- 2011
- 7:26 PM
- Leave a comment
Recently, I came across a pretty annoying issue with the FragmentPagerAdapter in the Android Compatibility Library (v4, released in October 2011). If you change any data in the child fragments of the ViewPager and call notifyDataSetChanged(), nothing happens. The PagerAdapter relies on data inside the FragmentManager instead of rebuilding fragments through the getItem() call. It seems others have faced this issue as well:
http://code.google.com/p/android/issues/detail?id=19001
Looking through the source code, it looks like there is no default DataSetObserver. Worse is the fact that the DataSetObserver interface is private to the Android Compatibility Library so you can’t even supply your own ! Luckily, a workaround can be found as follows:
Create a new class called PagerAdapterCompat with the package name android.support.v4.view
package android.support.v4.view;
public abstract class FragmentPagerAdapterCompat extends FragmentPagerAdapter {
public FragmentPagerAdapterCompat(FragmentManager fm) { super(fm); }
public interface DataSetObserver extends PagerAdapter.DataSetObserver {}
public static void setDataSetObserver(PagerAdapter adapter, DataSetObserver observer) {
adapter.setDataSetObserver(observer);
}
}
Don’t forget to hit Ctrl+Shift+O in eclipse to get those imports added
After this, you can simply create your own DataSetObserver (I made mine pretty basic by calling destroyItem() on each page) and set it with FragmentPagerAdapterCompat.setDataSetObserver(your_adapter, your_observer);
All credit goes to this guy for reporting the bug and providing a quick and easy solution: http://code.google.com/p/android/issues/detail?id=19110 (although his is more general for the PagerAdapter)
Please head on over there to star the bug so that it get’s more attention!


My name is Andrew Kwasik and welcome to my blog where you'll find my occasional ramblings. From the latest in tech news to some tips I uncover while working on my many hobbies.
or the contact page