FragmentStatePagerAdapter.java revision 7dc96cc2410f551eefaa973ddc144146ad72d1ec
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.v13.app; 18 19import android.app.Fragment; 20import android.app.FragmentManager; 21import android.app.FragmentTransaction; 22import android.os.Bundle; 23import android.os.Parcelable; 24import android.support.v4.app.FragmentPagerAdapter; 25import android.support.v4.view.PagerAdapter; 26import android.util.Log; 27import android.view.View; 28import android.view.ViewGroup; 29 30import java.util.ArrayList; 31 32/** 33 * Implementation of {@link android.support.v4.view.PagerAdapter} that 34 * uses a {@link Fragment} to manage each page. This class also handles 35 * saving and restoring of fragment's state. 36 * 37 * <p>This version of the pager is more useful when there are a large number 38 * of pages, working more like a list view. When pages are not visible to 39 * the user, their entire fragment may be destroyed, only keeping the saved 40 * state of that fragment. This allows the pager to hold on to much less 41 * memory associated with each visited page as compared to 42 * {@link FragmentPagerAdapter} at the cost of potentially more overhead when 43 * switching between pages. 44 * 45 * <p>When using FragmentPagerAdapter the host ViewPager must have a 46 * valid ID set.</p> 47 * 48 * <p>Subclasses only need to implement {@link #getItem(int)} 49 * and {@link #getCount()} to have a working adapter. 50 * 51 * <p>Here is an example implementation of a pager containing fragments of 52 * lists: 53 * 54 * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentStatePagerSupport.java 55 * complete} 56 * 57 * <p>The <code>R.layout.fragment_pager</code> resource of the top-level fragment is: 58 * 59 * {@sample development/samples/Support4Demos/res/layout/fragment_pager.xml 60 * complete} 61 * 62 * <p>The <code>R.layout.fragment_pager_list</code> resource containing each 63 * individual fragment's layout is: 64 * 65 * {@sample development/samples/Support4Demos/res/layout/fragment_pager_list.xml 66 * complete} 67 */ 68public abstract class FragmentStatePagerAdapter extends PagerAdapter { 69 private static final String TAG = "FragmentStatePagerAdapter"; 70 private static final boolean DEBUG = false; 71 72 private final FragmentManager mFragmentManager; 73 private FragmentTransaction mCurTransaction = null; 74 75 private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>(); 76 private ArrayList<Fragment> mFragments = new ArrayList<Fragment>(); 77 private Fragment mCurrentPrimaryItem = null; 78 79 public FragmentStatePagerAdapter(FragmentManager fm) { 80 mFragmentManager = fm; 81 } 82 83 /** 84 * Return the Fragment associated with a specified position. 85 */ 86 public abstract Fragment getItem(int position); 87 88 @Override 89 public void startUpdate(ViewGroup container) { 90 } 91 92 @Override 93 public Object instantiateItem(ViewGroup container, int position) { 94 // If we already have this item instantiated, there is nothing 95 // to do. This can happen when we are restoring the entire pager 96 // from its saved state, where the fragment manager has already 97 // taken care of restoring the fragments we previously had instantiated. 98 if (mFragments.size() > position) { 99 Fragment f = mFragments.get(position); 100 if (f != null) { 101 return f; 102 } 103 } 104 105 if (mCurTransaction == null) { 106 mCurTransaction = mFragmentManager.beginTransaction(); 107 } 108 109 Fragment fragment = getItem(position); 110 if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment); 111 if (mSavedState.size() > position) { 112 Fragment.SavedState fss = mSavedState.get(position); 113 if (fss != null) { 114 fragment.setInitialSavedState(fss); 115 } 116 } 117 while (mFragments.size() <= position) { 118 mFragments.add(null); 119 } 120 FragmentCompat.setMenuVisibility(fragment, false); 121 mFragments.set(position, fragment); 122 mCurTransaction.add(container.getId(), fragment); 123 124 return fragment; 125 } 126 127 @Override 128 public void destroyItem(ViewGroup container, int position, Object object) { 129 Fragment fragment = (Fragment)object; 130 131 if (mCurTransaction == null) { 132 mCurTransaction = mFragmentManager.beginTransaction(); 133 } 134 if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object 135 + " v=" + ((Fragment)object).getView()); 136 while (mSavedState.size() <= position) { 137 mSavedState.add(null); 138 } 139 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment)); 140 mFragments.set(position, null); 141 142 mCurTransaction.remove(fragment); 143 } 144 145 @Override 146 public void setPrimaryItem(ViewGroup container, int position, Object object) { 147 Fragment fragment = (Fragment)object; 148 if (fragment != mCurrentPrimaryItem) { 149 if (mCurrentPrimaryItem != null) { 150 FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false); 151 } 152 if (fragment != null) { 153 FragmentCompat.setMenuVisibility(fragment, true); 154 } 155 mCurrentPrimaryItem = fragment; 156 } 157 } 158 159 @Override 160 public void finishUpdate(ViewGroup container) { 161 if (mCurTransaction != null) { 162 mCurTransaction.commitAllowingStateLoss(); 163 mCurTransaction = null; 164 mFragmentManager.executePendingTransactions(); 165 } 166 } 167 168 @Override 169 public boolean isViewFromObject(View view, Object object) { 170 return ((Fragment)object).getView() == view; 171 } 172 173 @Override 174 public Parcelable saveState() { 175 Bundle state = null; 176 if (mSavedState.size() > 0) { 177 state = new Bundle(); 178 Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()]; 179 mSavedState.toArray(fss); 180 state.putParcelableArray("states", fss); 181 } 182 for (int i=0; i<mFragments.size(); i++) { 183 Fragment f = mFragments.get(i); 184 if (f != null) { 185 if (state == null) { 186 state = new Bundle(); 187 } 188 String key = "f" + i; 189 mFragmentManager.putFragment(state, key, f); 190 } 191 } 192 return state; 193 } 194 195 @Override 196 public void restoreState(Parcelable state, ClassLoader loader) { 197 if (state != null) { 198 Bundle bundle = (Bundle)state; 199 bundle.setClassLoader(loader); 200 Parcelable[] fss = bundle.getParcelableArray("states"); 201 mSavedState.clear(); 202 mFragments.clear(); 203 if (fss != null) { 204 for (int i=0; i<fss.length; i++) { 205 mSavedState.add((Fragment.SavedState)fss[i]); 206 } 207 } 208 Iterable<String> keys = bundle.keySet(); 209 for (String key: keys) { 210 if (key.startsWith("f")) { 211 int index = Integer.parseInt(key.substring(1)); 212 Fragment f = mFragmentManager.getFragment(bundle, key); 213 if (f != null) { 214 while (mFragments.size() <= index) { 215 mFragments.add(null); 216 } 217 FragmentCompat.setMenuVisibility(f, false); 218 mFragments.set(index, f); 219 } else { 220 Log.w(TAG, "Bad fragment at key " + key); 221 } 222 } 223 } 224 } 225 } 226} 227