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/**
26ff22d81f6561f6cdd2a91eb63238c41079927a22Kirill Grouchnikov * Implementation of {@link 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 *
47ce35f3b7736ff6e1c84bd5536e7c18922ab63c00Alan Viverette * {@sample frameworks/support/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 *
52ce35f3b7736ff6e1c84bd5536e7c18922ab63c00Alan Viverette * {@sample frameworks/support/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 *
58ce35f3b7736ff6e1c84bd5536e7c18922ab63c00Alan Viverette * {@sample frameworks/support/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) {
8036bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell        if (container.getId() == View.NO_ID) {
8136bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell            throw new IllegalStateException("ViewPager with adapter " + this
8236bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                    + " requires a view id");
8336bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell        }
845c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
855c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
865c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
87583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public Object instantiateItem(ViewGroup container, int position) {
885c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (mCurTransaction == null) {
895c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
905c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
915c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
921a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        final long itemId = getItemId(position);
931a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell
945c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        // Do we already have this fragment?
951a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        String name = makeFragmentName(container.getId(), itemId);
965c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        Fragment fragment = mFragmentManager.findFragmentByTag(name);
975c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (fragment != null) {
981a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell            if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
995c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction.attach(fragment);
1005c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        } else {
1015c1637087453de15e31861f073eae5133c4e9f7bDianne 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));
1055c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
1062a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
1072a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            fragment.setMenuVisibility(false);
10879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            fragment.setUserVisibleHint(false);
1092a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
1105c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1115c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return fragment;
1125c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1135c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1145c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
115583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void destroyItem(ViewGroup container, int position, Object object) {
1165c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (mCurTransaction == null) {
1175c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
1185c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
1191a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
1205c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn                + " v=" + ((Fragment)object).getView());
1215c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        mCurTransaction.detach((Fragment)object);
1225c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1235c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1245c1637087453de15e31861f073eae5133c4e9f7bDianne 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                mCurrentPrimaryItem.setMenuVisibility(false);
13079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                mCurrentPrimaryItem.setUserVisibleHint(false);
1312a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
1322a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment != null) {
1332a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                fragment.setMenuVisibility(true);
13479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                fragment.setUserVisibleHint(true);
1352a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
1362a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            mCurrentPrimaryItem = fragment;
1372a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
1382a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    }
1392a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
1402a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    @Override
141583d8a1ff64c7c59dd4e11759f3d8e994ce878d9Adam Powell    public void finishUpdate(ViewGroup container) {
1425c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (mCurTransaction != null) {
143e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell            mCurTransaction.commitNowAllowingStateLoss();
1445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction = null;
1455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
1465c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
1495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public boolean isViewFromObject(View view, Object object) {
1505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return ((Fragment)object).getView() == view;
1515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1525c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1535c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
1545c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public Parcelable saveState() {
1555c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return null;
1565c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1575c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
1595c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public void restoreState(Parcelable state, ClassLoader loader) {
1605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1621a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    /**
1631a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * Return a unique identifier for the item at the given position.
1641a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     *
1651a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * <p>The default implementation returns the given position.
1661a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * Subclasses should override this method if the positions of items can change.</p>
1671a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     *
1681a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * @param position Position within this adapter
1691a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     * @return Unique identifier for the item at position
1701a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell     */
1711a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    public long getItemId(int position) {
1721a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        return position;
1731a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    }
1741a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell
1751a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell    private static String makeFragmentName(int viewId, long id) {
1761a1c2acbc15f8bc9dba05d09dcb18e340474e1c6Adam Powell        return "android:switcher:" + viewId + ":" + id;
1775c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1785c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
179