FragmentPagerAdapter.javaAPI DocAndroid 5.1 API6478Thu Mar 12 22:22:56 GMT


public abstract class FragmentPagerAdapter extends
Implementation of {@link} that represents each page as a {@link} that is persistently kept in the fragment manager as long as the user can return to the page.

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider {@link FragmentStatePagerAdapter}.

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/Support13Demos/src/com/example/android/supportv13/app/ complete}

The R.layout.fragment_pager resource of the top-level fragment is: {@sample development/samples/Support13Demos/res/layout/fragment_pager.xml complete}

The R.layout.fragment_pager_list resource containing each individual fragment's layout is: {@sample development/samples/Support13Demos/res/layout/fragment_pager_list.xml complete}

Fields Summary
private static final String
private static final boolean
private final
Constructors Summary
public FragmentPagerAdapter( fm)

        mFragmentManager = fm;
Methods Summary
public voiddestroyItem(android.view.ViewGroup container, int position, java.lang.Object object)

        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
                + " v=" + ((Fragment)object).getView());
public voidfinishUpdate(android.view.ViewGroup container)

        if (mCurTransaction != null) {
            mCurTransaction = null;
public abstract position)
Return the Fragment associated with a specified position.

public longgetItemId(int position)
Return a unique identifier for the item at the given position.

The default implementation returns the given position. Subclasses should override this method if the positions of items can change.

position Position within this adapter
Unique identifier for the item at position

        return position;
public java.lang.ObjectinstantiateItem(android.view.ViewGroup container, int position)

        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();

        final long itemId = getItemId(position);

        // Do we already have this fragment?
        String name = makeFragmentName(container.getId(), itemId);
        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        if (fragment != null) {
            if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
        } else {
            fragment = getItem(position);
            if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
            mCurTransaction.add(container.getId(), fragment,
                    makeFragmentName(container.getId(), itemId));
        if (fragment != mCurrentPrimaryItem) {
            FragmentCompat.setMenuVisibility(fragment, false);
            FragmentCompat.setUserVisibleHint(fragment, false);

        return fragment;
public booleanisViewFromObject(android.view.View view, java.lang.Object object)

        return ((Fragment)object).getView() == view;
private static java.lang.StringmakeFragmentName(int viewId, long id)

        return "android:switcher:" + viewId + ":" + id;
public voidrestoreState(android.os.Parcelable state, java.lang.ClassLoader loader)

public android.os.ParcelablesaveState()

        return null;
public voidsetPrimaryItem(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 voidstartUpdate(android.view.ViewGroup container)