BaseFragmentPagerAdapterpublic 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.
- 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.
- The method {@link #isViewFromObject(View, Object)} has been reimplemented to search
the entire view hierarchy for the given view.
- 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_SIZEThe 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 | mFragmentCacheA cache to store detached fragments before they are removed |
Methods Summary |
---|
public void | destroyItem(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 void | finishUpdate(android.view.View container)
if (mCurTransaction != null && !mFragmentManager.isDestroyed()) {
mCurTransaction.commitAllowingStateLoss();
mCurTransaction = null;
mFragmentManager.executePendingTransactions();
}
| public abstract android.support.v4.app.Fragment | getItem(int position)Return the Fragment associated with a specified position.
| public java.lang.Object | instantiateItem(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 boolean | isViewFromObject(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.String | makeFragmentName(int viewId, int index)Creates a name for the fragment
return "android:switcher:" + viewId + ":" + index;
| public void | restoreState(android.os.Parcelable state, java.lang.ClassLoader loader)
| public android.os.Parcelable | saveState()
return null;
| public void | setPrimaryItem(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 void | startUpdate(android.view.View container)
|
|