FragmentPagerAdapter.java revision 7dc96cc2410f551eefaa973ddc144146ad72d1ec
1ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn/*
2ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * Copyright (C) 2011 The Android Open Source Project
3ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn *
4ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * you may not use this file except in compliance with the License.
6ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * You may obtain a copy of the License at
7ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn *
8ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn *
10ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * Unless required by applicable law or agreed to in writing, software
11ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * See the License for the specific language governing permissions and
14ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn * limitations under the License.
15ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn */
16ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
17ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackbornpackage android.support.v13.app;
18ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
19ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackbornimport android.app.Fragment;
20ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackbornimport android.app.FragmentManager;
21ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackbornimport android.app.FragmentTransaction;
22ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackbornimport android.os.Parcelable;
237dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackbornimport android.support.v4.app.FragmentStatePagerAdapter;
245c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornimport android.support.v4.view.PagerAdapter;
25ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackbornimport android.util.Log;
26ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackbornimport android.view.View;
27583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powellimport android.view.ViewGroup;
28ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn/**
305c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Implementation of {@link android.support.v4.view.PagerAdapter} that
315c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * represents each page as a {@link android.app.Fragment} that is persistently
325c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * kept in the fragment manager as long as the user can return to the page.
337dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
347dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>This version of the pager is best for use when there are a handful of
357dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * typically more static fragments to be paged through, such as a set of tabs.
367dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * The fragment of each page the user visits will be kept in memory, though its
377dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * view hierarchy may be destroyed when not visible.  This can result in using
387dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * a significant amount of memory since fragment instances can hold on to an
397dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * arbitrary amount of state.  For larger sets of pages, consider
407dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@link FragmentStatePagerAdapter}.
417dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
427dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>When using FragmentPagerAdapter the host ViewPager must have a
437dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * valid ID set.</p>
447dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
457dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>Subclasses only need to implement {@link #getItem(int)}
467dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * and {@link #getCount()} to have a working adapter.
477dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
487dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>Here is an example implementation of a pager containing fragments of
497dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * lists:
507dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
517dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@sample development/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentPagerSupport.java
527dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *      complete}
537dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
547dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>The <code>R.layout.fragment_pager</code> resource of the top-level fragment is:
557dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
567dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@sample development/samples/Support13Demos/res/layout/fragment_pager.xml
577dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *      complete}
587dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
597dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * <p>The <code>R.layout.fragment_pager_list</code> resource containing each
607dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * individual fragment's layout is:
617dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *
627dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn * {@sample development/samples/Support13Demos/res/layout/fragment_pager_list.xml
637dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn *      complete}
645c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */
655c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornpublic abstract class FragmentPagerAdapter extends PagerAdapter {
66ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    private static final String TAG = "FragmentPagerAdapter";
67ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    private static final boolean DEBUG = false;
68ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
69ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    private final FragmentManager mFragmentManager;
70ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    private FragmentTransaction mCurTransaction = null;
712a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    private Fragment mCurrentPrimaryItem = null;
72ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
73ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    public FragmentPagerAdapter(FragmentManager fm) {
74ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        mFragmentManager = fm;
75ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
76ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
77ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    /**
78ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn     * Return the Fragment associated with a specified position.
79ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn     */
80ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    public abstract Fragment getItem(int position);
81ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
82ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
83583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void startUpdate(ViewGroup container) {
84ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
85ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
86ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
87583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public Object instantiateItem(ViewGroup container, int position) {
88ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (mCurTransaction == null) {
89ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
90ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
91ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
92ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        // Do we already have this fragment?
935c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        String name = makeFragmentName(container.getId(), position);
94ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        Fragment fragment = mFragmentManager.findFragmentByTag(name);
95ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (fragment != null) {
96ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
97ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction.attach(fragment);
98ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        } else {
99ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            fragment = getItem(position);
100ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
1015c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction.add(container.getId(), fragment,
1025c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn                    makeFragmentName(container.getId(), position));
103ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
1042a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
1052a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            FragmentCompat.setMenuVisibility(fragment, false);
10679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            FragmentCompat.setUserVisibleHint(fragment, false);
1072a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
108ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
109ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return fragment;
110ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
111ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
112ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
113583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void destroyItem(ViewGroup container, int position, Object object) {
114ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (mCurTransaction == null) {
115ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
116ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
117ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object
118ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn                + " v=" + ((Fragment)object).getView());
119ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        mCurTransaction.detach((Fragment)object);
120ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
121ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
122ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
123583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void setPrimaryItem(ViewGroup container, int position, Object object) {
1242a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        Fragment fragment = (Fragment)object;
1252a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
1262a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (mCurrentPrimaryItem != null) {
1272a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false);
12879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem, false);
1292a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
1302a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment != null) {
1312a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                FragmentCompat.setMenuVisibility(fragment, true);
13279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                FragmentCompat.setUserVisibleHint(fragment, true);
1332a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
1342a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            mCurrentPrimaryItem = fragment;
1352a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
1362a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    }
1372a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
1382a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    @Override
139583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void finishUpdate(ViewGroup container) {
140ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (mCurTransaction != null) {
1412a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            mCurTransaction.commitAllowingStateLoss();
142ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction = null;
143ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mFragmentManager.executePendingTransactions();
144ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
145ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
146ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
147ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
148ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    public boolean isViewFromObject(View view, Object object) {
149ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return ((Fragment)object).getView() == view;
150ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
151ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
152ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
153ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    public Parcelable saveState() {
154ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return null;
155ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
156ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
157ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
1585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public void restoreState(Parcelable state, ClassLoader loader) {
159ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
160ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
161ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    private static String makeFragmentName(int viewId, int index) {
162ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return "android:switcher:" + viewId + ":" + index;
163ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
164ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn}