FileDocCategorySizeDatePackage
BaseFragmentPagerAdapter.javaAPI DocAndroid 5.1 API6897Thu Mar 12 22:22:52 GMT 2015com.android.ex.photo.adapters

BaseFragmentPagerAdapter

public abstract class BaseFragmentPagerAdapter extends android.support.v4.view.PagerAdapter
NOTE: This is a direct copy of {@link android.support.v4.app.FragmentPagerAdapter} with four very important modifications.

  1. The method {@link #makeFragmentName(int, int)} is declared "protected" in our class. We need to be able to re-define the fragment's name according to data only available to sub-classes.
  2. The method {@link #isViewFromObject(View, Object)} has been reimplemented to search the entire view hierarchy for the given view.
  3. In method {@link #destroyItem(View, int, Object)}, the fragment is detached and added to a cache. If the fragment is evicted from the cache, it will be deleted. An album may contain thousands of photos and we want to avoid having thousands of fragments.

Fields Summary
private static final int
DEFAULT_CACHE_SIZE
The default size of {@link #mFragmentCache}
private static final String
TAG
private static final boolean
DEBUG
private final android.support.v4.app.FragmentManager
mFragmentManager
private android.support.v4.app.FragmentTransaction
mCurTransaction
private android.support.v4.app.Fragment
mCurrentPrimaryItem
private android.support.v4.util.LruCache
mFragmentCache
A cache to store detached fragments before they are removed
Constructors Summary
public BaseFragmentPagerAdapter(android.support.v4.app.FragmentManager fm)


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

        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
        if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object
                + " v=" + ((Fragment)object).getView());

        Fragment fragment = (Fragment) object;
        String name = fragment.getTag();
        if (name == null) {
            // We prefer to get the name directly from the fragment, but, if the fragment is
            // detached before the add transaction is committed, this could be 'null'. In
            // that case, generate a name so we can still cache the fragment.
            name = makeFragmentName(container.getId(), position);
        }

        mFragmentCache.put(name, fragment);
        mCurTransaction.detach(fragment);
    
public voidfinishUpdate(android.view.View container)

        if (mCurTransaction != null && !mFragmentManager.isDestroyed()) {
            mCurTransaction.commitAllowingStateLoss();
            mCurTransaction = null;
            mFragmentManager.executePendingTransactions();
        }
    
public abstract android.support.v4.app.FragmentgetItem(int position)
Return the Fragment associated with a specified position.

public java.lang.ObjectinstantiateItem(android.view.View container, int position)

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

        // Do we already have this fragment?
        String name = makeFragmentName(container.getId(), position);

        // Remove item from the cache
        mFragmentCache.remove(name);

        Fragment fragment = mFragmentManager.findFragmentByTag(name);
        if (fragment != null) {
            if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
            mCurTransaction.attach(fragment);
        } else {
            fragment = getItem(position);
            if(fragment == null) {
                if (DEBUG) Log.e(TAG, "NPE workaround for getItem(). See b/7103023");
                return null;
            }
            if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
            mCurTransaction.add(container.getId(), fragment,
                    makeFragmentName(container.getId(), position));
        }
        if (fragment != mCurrentPrimaryItem) {
            fragment.setMenuVisibility(false);
        }

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

        // Ascend the tree to determine if the view is a child of the fragment
        View root = ((Fragment) object).getView();
        for (Object v = view; v instanceof View; v = ((View) v).getParent()) {
            if (v == root) {
                return true;
            }
        }
        return false;
    
protected java.lang.StringmakeFragmentName(int viewId, int index)
Creates a name for the fragment

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

    
public android.os.ParcelablesaveState()

        return null;
    
public voidsetPrimaryItem(android.view.View container, int position, java.lang.Object object)

        Fragment fragment = (Fragment) object;
        if (fragment != mCurrentPrimaryItem) {
            if (mCurrentPrimaryItem != null) {
                mCurrentPrimaryItem.setMenuVisibility(false);
            }
            if (fragment != null) {
                fragment.setMenuVisibility(true);
            }
            mCurrentPrimaryItem = fragment;
        }

    
public voidstartUpdate(android.view.View container)