15c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn/* 25c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Copyright (C) 2011 The Android Open Source Project 35c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * 45c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License"); 55c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * you may not use this file except in compliance with the License. 65c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * You may obtain a copy of the License at 75c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * 85c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * http://www.apache.org/licenses/LICENSE-2.0 95c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * 105c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Unless required by applicable law or agreed to in writing, software 115c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS, 125c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * See the License for the specific language governing permissions and 145c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * limitations under the License. 155c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */ 165c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 175c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornpackage android.support.v4.app; 185c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 195c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.os.Parcelable; 205c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.support.v4.view.PagerAdapter; 215c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.util.Log; 225c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.view.View; 23583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powellimport android.view.ViewGroup; 245c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 255c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn/** 265c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Implementation of {@link android.support.v4.view.PagerAdapter} that 275c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * represents each page as a {@link Fragment} that is persistently 285c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * kept in the fragment manager as long as the user can return to the page. 29583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell * 307dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>This version of the pager is best for use when there are a handful of 317dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * typically more static fragments to be paged through, such as a set of tabs. 327dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * The fragment of each page the user visits will be kept in memory, though its 337dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * view hierarchy may be destroyed when not visible. This can result in using 347dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * a significant amount of memory since fragment instances can hold on to an 357dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * arbitrary amount of state. For larger sets of pages, consider 367dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@link FragmentStatePagerAdapter}. 377dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 38583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell * <p>When using FragmentPagerAdapter the host ViewPager must have a 39583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell * valid ID set.</p> 407dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 417dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>Subclasses only need to implement {@link #getItem(int)} 427dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * and {@link #getCount()} to have a working adapter. 437dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 447dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>Here is an example implementation of a pager containing fragments of 457dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * lists: 467dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 477dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java 487dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * complete} 497dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 507dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>The <code>R.layout.fragment_pager</code> resource of the top-level fragment is: 517dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 527dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@sample development/samples/Support4Demos/res/layout/fragment_pager.xml 537dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * complete} 547dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 557dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>The <code>R.layout.fragment_pager_list</code> resource containing each 567dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * individual fragment's layout is: 577dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * 587dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@sample development/samples/Support4Demos/res/layout/fragment_pager_list.xml 597dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * complete} 605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */ 615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornpublic abstract class FragmentPagerAdapter extends PagerAdapter { 625c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn private static final String TAG = "FragmentPagerAdapter"; 635c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn private static final boolean DEBUG = false; 645c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 655c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn private final FragmentManager mFragmentManager; 665c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn private FragmentTransaction mCurTransaction = null; 672a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn private Fragment mCurrentPrimaryItem = null; 685c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 695c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public FragmentPagerAdapter(FragmentManager fm) { 705c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mFragmentManager = fm; 715c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 725c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 735c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn /** 745c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Return the Fragment associated with a specified position. 755c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */ 765c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public abstract Fragment getItem(int position); 775c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 785c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 79583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell public void startUpdate(ViewGroup container) { 805c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 815c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 825c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 83583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell public Object instantiateItem(ViewGroup container, int position) { 845c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (mCurTransaction == null) { 855c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mCurTransaction = mFragmentManager.beginTransaction(); 865c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 875c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 881a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell final long itemId = getItemId(position); 891a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell 905c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn // Do we already have this fragment? 911a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell String name = makeFragmentName(container.getId(), itemId); 925c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn Fragment fragment = mFragmentManager.findFragmentByTag(name); 935c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (fragment != null) { 941a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment); 955c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mCurTransaction.attach(fragment); 965c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } else { 975c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn fragment = getItem(position); 981a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment); 995c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mCurTransaction.add(container.getId(), fragment, 1001a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell makeFragmentName(container.getId(), itemId)); 1015c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1022a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment != mCurrentPrimaryItem) { 1032a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn fragment.setMenuVisibility(false); 10479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell fragment.setUserVisibleHint(false); 1052a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn } 1065c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1075c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return fragment; 1085c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1095c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1105c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 111583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell public void destroyItem(ViewGroup container, int position, Object object) { 1125c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (mCurTransaction == null) { 1135c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mCurTransaction = mFragmentManager.beginTransaction(); 1145c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1151a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object 1165c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn + " v=" + ((Fragment)object).getView()); 1175c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mCurTransaction.detach((Fragment)object); 1185c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1195c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1205c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 121583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell public void setPrimaryItem(ViewGroup container, int position, Object object) { 1222a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn Fragment fragment = (Fragment)object; 1232a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment != mCurrentPrimaryItem) { 1242a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (mCurrentPrimaryItem != null) { 1252a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn mCurrentPrimaryItem.setMenuVisibility(false); 12679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell mCurrentPrimaryItem.setUserVisibleHint(false); 1272a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn } 1282a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn if (fragment != null) { 1292a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn fragment.setMenuVisibility(true); 13079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell fragment.setUserVisibleHint(true); 1312a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn } 1322a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn mCurrentPrimaryItem = fragment; 1332a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn } 1342a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn } 1352a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn 1362a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn @Override 137583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell public void finishUpdate(ViewGroup container) { 1385c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn if (mCurTransaction != null) { 1392a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn mCurTransaction.commitAllowingStateLoss(); 1405c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mCurTransaction = null; 1415c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn mFragmentManager.executePendingTransactions(); 1425c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1435c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 1465c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public boolean isViewFromObject(View view, Object object) { 1475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return ((Fragment)object).getView() == view; 1485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 1515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public Parcelable saveState() { 1525c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn return null; 1535c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1545c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1555c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn @Override 1565c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn public void restoreState(Parcelable state, ClassLoader loader) { 1575c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn 1591a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell /** 1601a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell * Return a unique identifier for the item at the given position. 1611a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell * 1621a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell * <p>The default implementation returns the given position. 1631a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell * Subclasses should override this method if the positions of items can change.</p> 1641a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell * 1651a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell * @param position Position within this adapter 1661a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell * @return Unique identifier for the item at position 1671a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell */ 1681a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell public long getItemId(int position) { 1691a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell return position; 1701a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell } 1711a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell 1721a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell private static String makeFragmentName(int viewId, long id) { 1731a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell return "android:switcher:" + viewId + ":" + id; 1745c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn } 1755c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn} 176