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