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