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
921a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        final long itemId = getItemId(position);
931a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell
94ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        // Do we already have this fragment?
951a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        String name = makeFragmentName(container.getId(), itemId);
96ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        Fragment fragment = mFragmentManager.findFragmentByTag(name);
97ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (fragment != null) {
981a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell            if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
99ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction.attach(fragment);
100ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        } else {
101ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            fragment = getItem(position);
1021a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell            if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
1035c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction.add(container.getId(), fragment,
1041a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell                    makeFragmentName(container.getId(), itemId));
105ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
1062a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
1072a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            FragmentCompat.setMenuVisibility(fragment, false);
10879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            FragmentCompat.setUserVisibleHint(fragment, false);
1092a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
110ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
111ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return fragment;
112ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
113ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
114ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
115583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void destroyItem(ViewGroup container, int position, Object object) {
116ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (mCurTransaction == null) {
117ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
118ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
1191a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
120ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn                + " v=" + ((Fragment)object).getView());
121ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        mCurTransaction.detach((Fragment)object);
122ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
123ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
124ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
125583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void setPrimaryItem(ViewGroup container, int position, Object object) {
1262a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        Fragment fragment = (Fragment)object;
1272a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
1282a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (mCurrentPrimaryItem != null) {
1292a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false);
13079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem, false);
1312a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
1322a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment != null) {
1332a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                FragmentCompat.setMenuVisibility(fragment, true);
13479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                FragmentCompat.setUserVisibleHint(fragment, true);
1352a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
1362a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            mCurrentPrimaryItem = fragment;
1372a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
1382a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    }
1392a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
1402a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    @Override
141583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void finishUpdate(ViewGroup container) {
142ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (mCurTransaction != null) {
1432a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            mCurTransaction.commitAllowingStateLoss();
144ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction = null;
145ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mFragmentManager.executePendingTransactions();
146ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
147ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
148ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
149ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
150ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    public boolean isViewFromObject(View view, Object object) {
151ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return ((Fragment)object).getView() == view;
152ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
153ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
154ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
155ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    public Parcelable saveState() {
156ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return null;
157ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
158ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
159ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
1605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public void restoreState(Parcelable state, ClassLoader loader) {
161ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
162ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
1631a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    /**
1641a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * Return a unique identifier for the item at the given position.
1651a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     *
1661a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * <p>The default implementation returns the given position.
1671a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * Subclasses should override this method if the positions of items can change.</p>
1681a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     *
1691a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * @param position Position within this adapter
1701a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * @return Unique identifier for the item at position
1711a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     */
1721a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    public long getItemId(int position) {
1731a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        return position;
1741a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    }
1751a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell
1761a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    private static String makeFragmentName(int viewId, long id) {
1771a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        return "android:switcher:" + viewId + ":" + id;
178ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
179ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn}