I have a fragment that contains a ViewPager
. This ViewPager
is backed by a PagerAdapter
that uses a Cursor
. The cursor is managed by LoaderCallbacks
. I'm using the v4 support libraries here.
What I want is to create the fragment and have the view pager showing a specified page, rather than starting at page 0.
I know that ViewPager
has a setCurrentItem()
method, but the data may not yet be loaded when the ViewPager
is created. What I need is to listen to the adapter for data set changes, and if that is the first such change, then call setCurrentItem()
on ViewPager
.
However the PagerAdapter
class does not export the registerDataSetObserver()
method; it has package
rather than public
access (at least in the v4 support library).
What I have done, and it seems very much like a hack to me, is the following:
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() {
// Set the current page of the pager if (a) this is the
// first time attempting to set the page and (b) the
// pager exists and (c) the adapter has data.
if (!initialPageSet && mPager != null && mAdapter.getCount() > 0) {
mPager.setCurrentItem(mInitialPageToShow);
initialPageSet = true;
}
}
}
There's a race condition between loading data into the adapter (in the onCreate()
method), and creating the ViewPager
in the onCreateView()
method. So the current page can be set either when (a) the pager exists but data is loaded for the first time or (b) the data is loaded before the pager is created.
I've tried to handle both cases above but I'm thinking there must be an alternative approach that is more robust (and hopefully simpler) using observers.
Wait to initLoader
until after the pager exists, check the boolean flag and do setCurrentItem
only the first time onLoadFinished
is called:
class ItemPagerFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private CursorPagerAdapter mAdapter;
private ViewPager mPager;
private int mInitialPageToShow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@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);
mAdapter = new CursorPagerAdapter(getFragmentManager());
mPager.setAdapter(mAdapter);
return view;
}
@Override
public void onViewCreated(View view, Bundle saved) {
super.onViewCreated(view, saved);
...
getLoaderManager().initLoader(LOADER_ID, null, this);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
mAdapter.swapCursor(cursor);
setInitialPageIfRequired();
}
private boolean initialPageSet = false;
private synchronized void setInitialPageIfRequired() {
// Set the current page of the pager if (a) this is the
// first time attempting to set the page and (b) the
// pager exists and (c) the adapter has data.
if (!initialPageSet && mPager != null && mAdapter.getCount() > 0) {
mPager.setCurrentItem(mInitialPageToShow);
initialPageSet = true;
}
}
}
Note: this snippet assumes v13 support library, but it should be the same for v4.
来源:https://stackoverflow.com/questions/13815010/showing-specified-page-when-view-pager-is-first-created