FragmentPagerAdapter.java revision 2a4d8518f36346ea25a22a736453ff28f2954165
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;
235c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
245c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn/**
255c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * Implementation of {@link android.support.v4.view.PagerAdapter} that
265c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * represents each page as a {@link Fragment} that is persistently
275c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn * kept in the fragment manager as long as the user can return to the page.
285c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn */
295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackbornpublic abstract class FragmentPagerAdapter extends PagerAdapter {
305c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    private static final String TAG = "FragmentPagerAdapter";
315c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    private static final boolean DEBUG = false;
325c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
335c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    private final FragmentManager mFragmentManager;
345c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    private FragmentTransaction mCurTransaction = null;
352a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    private Fragment mCurrentPrimaryItem = null;
365c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
375c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public FragmentPagerAdapter(FragmentManager fm) {
385c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        mFragmentManager = fm;
395c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
405c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
415c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
425c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Return the Fragment associated with a specified position.
435c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract Fragment getItem(int position);
455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
465c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public void startUpdate(View container) {
485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public Object instantiateItem(View container, int position) {
525c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (mCurTransaction == null) {
535c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
545c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
555c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
565c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        // Do we already have this fragment?
575c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        String name = makeFragmentName(container.getId(), position);
585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        Fragment fragment = mFragmentManager.findFragmentByTag(name);
595c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (fragment != null) {
605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction.attach(fragment);
625c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        } else {
635c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            fragment = getItem(position);
645c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
655c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction.add(container.getId(), fragment,
665c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn                    makeFragmentName(container.getId(), position));
675c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
682a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
692a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            fragment.setMenuVisibility(false);
702a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
715c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
725c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return fragment;
735c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
745c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
755c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
765c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public void destroyItem(View container, int position, Object object) {
775c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (mCurTransaction == null) {
785c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction = mFragmentManager.beginTransaction();
795c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
805c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object
815c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn                + " v=" + ((Fragment)object).getView());
825c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        mCurTransaction.detach((Fragment)object);
835c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
845c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
855c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
862a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    public void setPrimaryItem(View container, int position, Object object) {
872a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        Fragment fragment = (Fragment)object;
882a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        if (fragment != mCurrentPrimaryItem) {
892a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (mCurrentPrimaryItem != null) {
902a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                mCurrentPrimaryItem.setMenuVisibility(false);
912a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
922a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment != null) {
932a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                fragment.setMenuVisibility(true);
942a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            }
952a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            mCurrentPrimaryItem = fragment;
962a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        }
972a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    }
982a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
992a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn    @Override
1005c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public void finishUpdate(View container) {
1015c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (mCurTransaction != null) {
1022a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            mCurTransaction.commitAllowingStateLoss();
1035c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mCurTransaction = null;
1045c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mFragmentManager.executePendingTransactions();
1055c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
1065c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1075c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1085c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
1095c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public boolean isViewFromObject(View view, Object object) {
1105c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return ((Fragment)object).getView() == view;
1115c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1125c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1135c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
1145c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public Parcelable saveState() {
1155c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return null;
1165c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1175c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1185c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
1195c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public void restoreState(Parcelable state, ClassLoader loader) {
1205c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1215c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1225c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    private static String makeFragmentName(int viewId, int index) {
1235c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return "android:switcher:" + viewId + ":" + index;
1245c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
1255c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn}
126