I have a fragment containing a ViewPager . This ViewPager supported by the PagerAdapter , which uses Cursor . The cursor is controlled by LoaderCallbacks . I use v4 support libraries here.
I want to create a fragment and show the presentation pager with the specified page, and not start from page 0.
I know that ViewPager has a setCurrentItem() method, but the data may not load yet when creating the ViewPager . I need to listen to the adapter for changes to the dataset, and if this is the first such change, call setCurrentItem() on the ViewPager .
However, the PagerAdapter class PagerAdapter not export the registerDataSetObserver() method; it has package , not public access (at least in the v4 support library).
What I did, and it looks like a hack to me, is this:
class ItemPagerFragment extends SherlockFragment implements LoaderCallbacks<Cursor> { private CursorPagerAdapter mAdapter; private ViewPager mPager; private int mInitialPageToShow; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAdapter = new CursorPagerAdapter() { @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); setInitialPageIfRequired(); } }; getActivity().getSupportLoaderManager().initLoader(LOADER_ID, null, this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) { View view = inflater.inflate(R.layout.items_pager, group, false); mPager = (ViewPager) view.findViewById(R.id.pager); mPager.setAdapter(mAdapter); setInitialPageIfRequired(); return view; } private boolean initialPageSet = false; private synchronized void setInitialPageIfRequired() {
There is a race condition between loading data into the adapter (in the onCreate() method) and creating a ViewPager in the onCreateView() method. Thus, the current page can be set when (a) a pager exists, but data is loaded for the first time or (b) data is loaded before the pager is created.
I tried to handle both cases above, but I think there should be an alternative approach, more reliable (and, hopefully, simpler) using observers.
android android-viewpager android-pageradapter android-cursorloader
John Q Citizen Dec 11 2018-12-12T00: 00Z
source share