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 *
51ce35f3b7736ff6e1c84bd5536e7c18922ab63c00Alan Viverette * {@sample frameworks/support/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 *
56ce35f3b7736ff6e1c84bd5536e7c18922ab63c00Alan Viverette * {@sample frameworks/support/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 *
62ce35f3b7736ff6e1c84bd5536e7c18922ab63c00Alan Viverette * {@sample frameworks/support/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) {
8436bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell        if (container.getId() == View.NO_ID) {
8536bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell            throw new IllegalStateException("ViewPager with adapter " + this
8636bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                    + " requires a view id");
8736bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell        }
88ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
89ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
90ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
91583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public Object instantiateItem(ViewGroup container, int position) {
92ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (mCurTransaction == null) {
93ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
94ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
95ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
961a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        final long itemId = getItemId(position);
971a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell
98ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        // Do we already have this fragment?
991a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        String name = makeFragmentName(container.getId(), itemId);
100ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        Fragment fragment = mFragmentManager.findFragmentByTag(name);
101ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (fragment != null) {
1021a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell            if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
103ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction.attach(fragment);
104ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        } else {
105ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            fragment = getItem(position);
1061a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell            if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
1075c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction.add(container.getId(), fragment,
1081a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell                    makeFragmentName(container.getId(), itemId));
109ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
1102a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
1112a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            FragmentCompat.setMenuVisibility(fragment, false);
11279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            FragmentCompat.setUserVisibleHint(fragment, false);
1132a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
114ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
115ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return fragment;
116ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
117ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
118ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
119583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void destroyItem(ViewGroup container, int position, Object object) {
120ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (mCurTransaction == null) {
121ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
122ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
1231a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
124ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn                + " v=" + ((Fragment)object).getView());
125ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        mCurTransaction.detach((Fragment)object);
126ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
127ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
128ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
129583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void setPrimaryItem(ViewGroup container, int position, Object object) {
1302a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        Fragment fragment = (Fragment)object;
1312a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
1322a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (mCurrentPrimaryItem != null) {
1332a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false);
13479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem, false);
1352a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
1362a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment != null) {
1372a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                FragmentCompat.setMenuVisibility(fragment, true);
13879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                FragmentCompat.setUserVisibleHint(fragment, true);
1392a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
1402a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            mCurrentPrimaryItem = fragment;
1412a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
1422a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    }
1432a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
1442a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    @Override
145583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void finishUpdate(ViewGroup container) {
146ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        if (mCurTransaction != null) {
14759785a64414fbfebbaa831a9c7d085a0a6fa4c3cAdam Powell            mCurTransaction.commitAllowingStateLoss();
148ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mCurTransaction = null;
14959785a64414fbfebbaa831a9c7d085a0a6fa4c3cAdam Powell            mFragmentManager.executePendingTransactions();
150ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        }
151ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
152ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
153ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
154ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    public boolean isViewFromObject(View view, Object object) {
155ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return ((Fragment)object).getView() == view;
156ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
157ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
158ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
159ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    public Parcelable saveState() {
160ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        return null;
161ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
162ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
163ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    @Override
1645c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public void restoreState(Parcelable state, ClassLoader loader) {
165ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
166ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn
1671a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    /**
1681a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * Return a unique identifier for the item at the given position.
1691a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     *
1701a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * <p>The default implementation returns the given position.
1711a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * Subclasses should override this method if the positions of items can change.</p>
1721a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     *
1731a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * @param position Position within this adapter
1741a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * @return Unique identifier for the item at position
1751a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     */
1761a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    public long getItemId(int position) {
1771a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        return position;
1781a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    }
1791a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell
1801a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    private static String makeFragmentName(int viewId, long id) {
1811a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        return "android:switcher:" + viewId + ":" + id;
182ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn    }
183ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn}