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; 23import android.view.ViewGroup; 24 25/** 26 * Implementation of {@link PagerAdapter} that 27 * represents each page as a {@link Fragment} that is persistently 28 * kept in the fragment manager as long as the user can return to the page. 29 * 30 * <p>This version of the pager is best for use when there are a handful of 31 * typically more static fragments to be paged through, such as a set of tabs. 32 * The fragment of each page the user visits will be kept in memory, though its 33 * view hierarchy may be destroyed when not visible. This can result in using 34 * a significant amount of memory since fragment instances can hold on to an 35 * arbitrary amount of state. For larger sets of pages, consider 36 * {@link FragmentStatePagerAdapter}. 37 * 38 * <p>When using FragmentPagerAdapter the host ViewPager must have a 39 * valid ID set.</p> 40 * 41 * <p>Subclasses only need to implement {@link #getItem(int)} 42 * and {@link #getCount()} to have a working adapter. 43 * 44 * <p>Here is an example implementation of a pager containing fragments of 45 * lists: 46 * 47 * {@sample frameworks/support/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java 48 * complete} 49 * 50 * <p>The <code>R.layout.fragment_pager</code> resource of the top-level fragment is: 51 * 52 * {@sample frameworks/support/samples/Support4Demos/res/layout/fragment_pager.xml 53 * complete} 54 * 55 * <p>The <code>R.layout.fragment_pager_list</code> resource containing each 56 * individual fragment's layout is: 57 * 58 * {@sample frameworks/support/samples/Support4Demos/res/layout/fragment_pager_list.xml 59 * complete} 60 */ 61public abstract class FragmentPagerAdapter extends PagerAdapter { 62 private static final String TAG = "FragmentPagerAdapter"; 63 private static final boolean DEBUG = false; 64 65 private final FragmentManager mFragmentManager; 66 private FragmentTransaction mCurTransaction = null; 67 private Fragment mCurrentPrimaryItem = null; 68 69 public FragmentPagerAdapter(FragmentManager fm) { 70 mFragmentManager = fm; 71 } 72 73 /** 74 * Return the Fragment associated with a specified position. 75 */ 76 public abstract Fragment getItem(int position); 77 78 @Override 79 public void startUpdate(ViewGroup container) { 80 if (container.getId() == View.NO_ID) { 81 throw new IllegalStateException("ViewPager with adapter " + this 82 + " requires a view id"); 83 } 84 } 85 86 @SuppressWarnings("ReferenceEquality") 87 @Override 88 public Object instantiateItem(ViewGroup container, int position) { 89 if (mCurTransaction == null) { 90 mCurTransaction = mFragmentManager.beginTransaction(); 91 } 92 93 final long itemId = getItemId(position); 94 95 // Do we already have this fragment? 96 String name = makeFragmentName(container.getId(), itemId); 97 Fragment fragment = mFragmentManager.findFragmentByTag(name); 98 if (fragment != null) { 99 if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment); 100 mCurTransaction.attach(fragment); 101 } else { 102 fragment = getItem(position); 103 if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment); 104 mCurTransaction.add(container.getId(), fragment, 105 makeFragmentName(container.getId(), itemId)); 106 } 107 if (fragment != mCurrentPrimaryItem) { 108 fragment.setMenuVisibility(false); 109 fragment.setUserVisibleHint(false); 110 } 111 112 return fragment; 113 } 114 115 @Override 116 public void destroyItem(ViewGroup container, int position, Object object) { 117 if (mCurTransaction == null) { 118 mCurTransaction = mFragmentManager.beginTransaction(); 119 } 120 if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object 121 + " v=" + ((Fragment)object).getView()); 122 mCurTransaction.detach((Fragment)object); 123 } 124 125 @SuppressWarnings("ReferenceEquality") 126 @Override 127 public void setPrimaryItem(ViewGroup container, int position, Object object) { 128 Fragment fragment = (Fragment)object; 129 if (fragment != mCurrentPrimaryItem) { 130 if (mCurrentPrimaryItem != null) { 131 mCurrentPrimaryItem.setMenuVisibility(false); 132 mCurrentPrimaryItem.setUserVisibleHint(false); 133 } 134 if (fragment != null) { 135 fragment.setMenuVisibility(true); 136 fragment.setUserVisibleHint(true); 137 } 138 mCurrentPrimaryItem = fragment; 139 } 140 } 141 142 @Override 143 public void finishUpdate(ViewGroup container) { 144 if (mCurTransaction != null) { 145 mCurTransaction.commitNowAllowingStateLoss(); 146 mCurTransaction = null; 147 } 148 } 149 150 @Override 151 public boolean isViewFromObject(View view, Object object) { 152 return ((Fragment)object).getView() == view; 153 } 154 155 @Override 156 public Parcelable saveState() { 157 return null; 158 } 159 160 @Override 161 public void restoreState(Parcelable state, ClassLoader loader) { 162 } 163 164 /** 165 * Return a unique identifier for the item at the given position. 166 * 167 * <p>The default implementation returns the given position. 168 * Subclasses should override this method if the positions of items can change.</p> 169 * 170 * @param position Position within this adapter 171 * @return Unique identifier for the item at position 172 */ 173 public long getItemId(int position) { 174 return position; 175 } 176 177 private static String makeFragmentName(int viewId, long id) { 178 return "android:switcher:" + viewId + ":" + id; 179 } 180} 181