FragmentStatePagerAdapterpublic abstract class FragmentStatePagerAdapter extends android.support.v4.view.PagerAdapter Implementation of {@link android.support.v4.view.PagerAdapter} that
uses a {@link Fragment} to manage each page. This class also handles
saving and restoring of fragment's state.
This version of the pager is more useful when there are a large number
of pages, working more like a list view. When pages are not visible to
the user, their entire fragment may be destroyed, only keeping the saved
state of that fragment. This allows the pager to hold on to much less
memory associated with each visited page as compared to
{@link FragmentPagerAdapter} at the cost of potentially more overhead when
switching between pages.
When using FragmentPagerAdapter the host ViewPager must have a
valid ID set.
Subclasses only need to implement {@link #getItem(int)}
and {@link #getCount()} to have a working adapter.
Here is an example implementation of a pager containing fragments of
lists:
{@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.java
complete}
The R.layout.fragment_pager resource of the top-level fragment is:
{@sample development/samples/Support4Demos/res/layout/fragment_pager.xml
complete}
The R.layout.fragment_pager_list resource containing each
individual fragment's layout is:
{@sample development/samples/Support4Demos/res/layout/fragment_pager_list.xml
complete} |
Fields Summary |
---|
private static final String | TAG | private static final boolean | DEBUG | private final android.app.FragmentManager | mFragmentManager | private android.app.FragmentTransaction | mCurTransaction | private ArrayList | mSavedState | private ArrayList | mFragments | private android.app.Fragment | mCurrentPrimaryItem |
Methods Summary |
---|
public void | destroyItem(android.view.ViewGroup container, int position, java.lang.Object object)
Fragment fragment = (Fragment)object;
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
+ " v=" + ((Fragment)object).getView());
while (mSavedState.size() <= position) {
mSavedState.add(null);
}
mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
mFragments.set(position, null);
mCurTransaction.remove(fragment);
| public void | finishUpdate(android.view.ViewGroup container)
if (mCurTransaction != null) {
mCurTransaction.commitAllowingStateLoss();
mCurTransaction = null;
mFragmentManager.executePendingTransactions();
}
| public abstract android.app.Fragment | getItem(int position)Return the Fragment associated with a specified position.
| public java.lang.Object | instantiateItem(android.view.ViewGroup container, int position)
// If we already have this item instantiated, there is nothing
// to do. This can happen when we are restoring the entire pager
// from its saved state, where the fragment manager has already
// taken care of restoring the fragments we previously had instantiated.
if (mFragments.size() > position) {
Fragment f = mFragments.get(position);
if (f != null) {
return f;
}
}
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
Fragment fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
if (mSavedState.size() > position) {
Fragment.SavedState fss = mSavedState.get(position);
if (fss != null) {
fragment.setInitialSavedState(fss);
}
}
while (mFragments.size() <= position) {
mFragments.add(null);
}
FragmentCompat.setMenuVisibility(fragment, false);
FragmentCompat.setUserVisibleHint(fragment, false);
mFragments.set(position, fragment);
mCurTransaction.add(container.getId(), fragment);
return fragment;
| public boolean | isViewFromObject(android.view.View view, java.lang.Object object)
return ((Fragment)object).getView() == view;
| public void | restoreState(android.os.Parcelable state, java.lang.ClassLoader loader)
if (state != null) {
Bundle bundle = (Bundle)state;
bundle.setClassLoader(loader);
Parcelable[] fss = bundle.getParcelableArray("states");
mSavedState.clear();
mFragments.clear();
if (fss != null) {
for (int i=0; i<fss.length; i++) {
mSavedState.add((Fragment.SavedState)fss[i]);
}
}
Iterable<String> keys = bundle.keySet();
for (String key: keys) {
if (key.startsWith("f")) {
int index = Integer.parseInt(key.substring(1));
Fragment f = mFragmentManager.getFragment(bundle, key);
if (f != null) {
while (mFragments.size() <= index) {
mFragments.add(null);
}
FragmentCompat.setMenuVisibility(f, false);
mFragments.set(index, f);
} else {
Log.w(TAG, "Bad fragment at key " + key);
}
}
}
}
| public android.os.Parcelable | saveState()
Bundle state = null;
if (mSavedState.size() > 0) {
state = new Bundle();
Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
mSavedState.toArray(fss);
state.putParcelableArray("states", fss);
}
for (int i=0; i<mFragments.size(); i++) {
Fragment f = mFragments.get(i);
if (f != null) {
if (state == null) {
state = new Bundle();
}
String key = "f" + i;
mFragmentManager.putFragment(state, key, f);
}
}
return state;
| public void | setPrimaryItem(android.view.ViewGroup container, int position, java.lang.Object object)
Fragment fragment = (Fragment)object;
if (fragment != mCurrentPrimaryItem) {
if (mCurrentPrimaryItem != null) {
FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false);
FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem, false);
}
if (fragment != null) {
FragmentCompat.setMenuVisibility(fragment, true);
FragmentCompat.setUserVisibleHint(fragment, true);
}
mCurrentPrimaryItem = fragment;
}
| public void | startUpdate(android.view.ViewGroup container)
|
|