FragmentPagerAdapter.java revision abc968f1eba800c34a4008deb43b015da5d23a5f
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.app; 18 19import android.os.Parcelable; 20import android.support.v4.view.PagerAdapter; 21import android.util.Log; 22import android.view.View; 23 24/** 25 * Implementation of {@link android.support.v4.view.PagerAdapter} that 26 * represents each page as a {@link Fragment} that is persistently 27 * kept in the fragment manager as long as the user can return to the page. 28 */ 29public abstract class FragmentPagerAdapter extends PagerAdapter { 30 private static final String TAG = "FragmentPagerAdapter"; 31 private static final boolean DEBUG = false; 32 33 private final FragmentManager mFragmentManager; 34 private FragmentTransaction mCurTransaction = null; 35 private Fragment mCurrentPrimaryItem = null; 36 37 public FragmentPagerAdapter(FragmentManager fm) { 38 mFragmentManager = fm; 39 } 40 41 /** 42 * Return the Fragment associated with a specified position. 43 */ 44 public abstract Fragment getItem(int position); 45 46 @Override 47 public void startUpdate(View container) { 48 } 49 50 @Override 51 public Object instantiateItem(View container, int position) { 52 if (mCurTransaction == null) { 53 mCurTransaction = mFragmentManager.beginTransaction(); 54 } 55 56 // Do we already have this fragment? 57 String name = makeFragmentName(container.getId(), position); 58 Fragment fragment = mFragmentManager.findFragmentByTag(name); 59 if (fragment != null) { 60 if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment); 61 mCurTransaction.attach(fragment); 62 } else { 63 fragment = getItem(position); 64 if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment); 65 mCurTransaction.add(container.getId(), fragment, 66 makeFragmentName(container.getId(), position)); 67 } 68 if (fragment != mCurrentPrimaryItem) { 69 fragment.setMenuVisibility(false); 70 fragment.setStartDeferred(true); 71 } 72 73 return fragment; 74 } 75 76 @Override 77 public void destroyItem(View container, int position, Object object) { 78 if (mCurTransaction == null) { 79 mCurTransaction = mFragmentManager.beginTransaction(); 80 } 81 if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object 82 + " v=" + ((Fragment)object).getView()); 83 mCurTransaction.detach((Fragment)object); 84 } 85 86 @Override 87 public void setPrimaryItem(View container, int position, Object object) { 88 Fragment fragment = (Fragment)object; 89 if (fragment != mCurrentPrimaryItem) { 90 if (mCurrentPrimaryItem != null) { 91 mCurrentPrimaryItem.setMenuVisibility(false); 92 mCurrentPrimaryItem.setStartDeferred(true); 93 } 94 if (fragment != null) { 95 fragment.setMenuVisibility(true); 96 fragment.setStartDeferred(false); 97 } 98 mCurrentPrimaryItem = fragment; 99 } 100 } 101 102 @Override 103 public void finishUpdate(View container) { 104 if (mCurTransaction != null) { 105 mCurTransaction.commitAllowingStateLoss(); 106 mCurTransaction = null; 107 mFragmentManager.executePendingTransactions(); 108 } 109 } 110 111 @Override 112 public boolean isViewFromObject(View view, Object object) { 113 return ((Fragment)object).getView() == view; 114 } 115 116 @Override 117 public Parcelable saveState() { 118 return null; 119 } 120 121 @Override 122 public void restoreState(Parcelable state, ClassLoader loader) { 123 } 124 125 private static String makeFragmentName(int viewId, int index) { 126 return "android:switcher:" + viewId + ":" + index; 127 } 128} 129