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 17 package android.support.v13.app; 18 19 import android.app.Fragment; 20 import android.app.FragmentManager; 21 import android.app.FragmentTransaction; 22 import android.os.Bundle; 23 import android.os.Parcelable; 24 import android.support.v4.app.FragmentPagerAdapter; 25 import android.support.v4.view.PagerAdapter; 26 import android.util.Log; 27 import android.view.View; 28 import android.view.ViewGroup; 29 30 import 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 */ 68 public 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 FragmentStatePagerAdapter(FragmentManager fm)79 public FragmentStatePagerAdapter(FragmentManager fm) { 80 mFragmentManager = fm; 81 } 82 83 /** 84 * Return the Fragment associated with a specified position. 85 */ getItem(int position)86 public abstract Fragment getItem(int position); 87 88 @Override startUpdate(ViewGroup container)89 public void startUpdate(ViewGroup container) { 90 } 91 92 @Override instantiateItem(ViewGroup container, int position)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 FragmentCompat.setUserVisibleHint(fragment, false); 122 mFragments.set(position, fragment); 123 mCurTransaction.add(container.getId(), fragment); 124 125 return fragment; 126 } 127 128 @Override destroyItem(ViewGroup container, int position, Object object)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, mFragmentManager.saveFragmentInstanceState(fragment)); 141 mFragments.set(position, null); 142 143 mCurTransaction.remove(fragment); 144 } 145 146 @Override setPrimaryItem(ViewGroup container, int position, Object object)147 public void setPrimaryItem(ViewGroup container, int position, Object object) { 148 Fragment fragment = (Fragment)object; 149 if (fragment != mCurrentPrimaryItem) { 150 if (mCurrentPrimaryItem != null) { 151 FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false); 152 FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem, false); 153 } 154 if (fragment != null) { 155 FragmentCompat.setMenuVisibility(fragment, true); 156 FragmentCompat.setUserVisibleHint(fragment, true); 157 } 158 mCurrentPrimaryItem = fragment; 159 } 160 } 161 162 @Override finishUpdate(ViewGroup container)163 public void finishUpdate(ViewGroup container) { 164 if (mCurTransaction != null) { 165 mCurTransaction.commitAllowingStateLoss(); 166 mCurTransaction = null; 167 mFragmentManager.executePendingTransactions(); 168 } 169 } 170 171 @Override isViewFromObject(View view, Object object)172 public boolean isViewFromObject(View view, Object object) { 173 return ((Fragment)object).getView() == view; 174 } 175 176 @Override saveState()177 public Parcelable saveState() { 178 Bundle state = null; 179 if (mSavedState.size() > 0) { 180 state = new Bundle(); 181 Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()]; 182 mSavedState.toArray(fss); 183 state.putParcelableArray("states", fss); 184 } 185 for (int i=0; i<mFragments.size(); i++) { 186 Fragment f = mFragments.get(i); 187 if (f != null && f.isAdded()) { 188 if (state == null) { 189 state = new Bundle(); 190 } 191 String key = "f" + i; 192 mFragmentManager.putFragment(state, key, f); 193 } 194 } 195 return state; 196 } 197 198 @Override restoreState(Parcelable state, ClassLoader loader)199 public void restoreState(Parcelable state, ClassLoader loader) { 200 if (state != null) { 201 Bundle bundle = (Bundle)state; 202 bundle.setClassLoader(loader); 203 Parcelable[] fss = bundle.getParcelableArray("states"); 204 mSavedState.clear(); 205 mFragments.clear(); 206 if (fss != null) { 207 for (int i=0; i<fss.length; i++) { 208 mSavedState.add((Fragment.SavedState)fss[i]); 209 } 210 } 211 Iterable<String> keys = bundle.keySet(); 212 for (String key: keys) { 213 if (key.startsWith("f")) { 214 int index = Integer.parseInt(key.substring(1)); 215 Fragment f = mFragmentManager.getFragment(bundle, key); 216 if (f != null) { 217 while (mFragments.size() <= index) { 218 mFragments.add(null); 219 } 220 FragmentCompat.setMenuVisibility(f, false); 221 mFragments.set(index, f); 222 } else { 223 Log.w(TAG, "Bad fragment at key " + key); 224 } 225 } 226 } 227 } 228 } 229 } 230