FragmentPagerAdapter.java revision 583d8a1ff64c7c59dd4e11759f3d8e994ce878d9
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v4.app;
18
19import android.os.Parcelable;
20import android.support.v4.view.PagerAdapter;
21import android.util.Log;
22import android.view.View;
23import android.view.ViewGroup;
24
25/**
26 * Implementation of {@link android.support.v4.view.PagerAdapter} that
27 * represents each page as a {@link Fragment} that is persistently
28 * kept in the fragment manager as long as the user can return to the page.
29 *
30 * <p>When using FragmentPagerAdapter the host ViewPager must have a
31 * valid ID set.</p>
32 */
33public abstract class FragmentPagerAdapter extends PagerAdapter {
34    private static final String TAG = "FragmentPagerAdapter";
35    private static final boolean DEBUG = false;
36
37    private final FragmentManager mFragmentManager;
38    private FragmentTransaction mCurTransaction = null;
39    private Fragment mCurrentPrimaryItem = null;
40
41    public FragmentPagerAdapter(FragmentManager fm) {
42        mFragmentManager = fm;
43    }
44
45    /**
46     * Return the Fragment associated with a specified position.
47     */
48    public abstract Fragment getItem(int position);
49
50    @Override
51    public void startUpdate(ViewGroup container) {
52    }
53
54    @Override
55    public Object instantiateItem(ViewGroup container, int position) {
56        if (mCurTransaction == null) {
57            mCurTransaction = mFragmentManager.beginTransaction();
58        }
59
60        // Do we already have this fragment?
61        String name = makeFragmentName(container.getId(), position);
62        Fragment fragment = mFragmentManager.findFragmentByTag(name);
63        if (fragment != null) {
64            if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
65            mCurTransaction.attach(fragment);
66        } else {
67            fragment = getItem(position);
68            if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
69            mCurTransaction.add(container.getId(), fragment,
70                    makeFragmentName(container.getId(), position));
71        }
72        if (fragment != mCurrentPrimaryItem) {
73            fragment.setMenuVisibility(false);
74            fragment.setUserVisibleHint(false);
75        }
76
77        return fragment;
78    }
79
80    @Override
81    public void destroyItem(ViewGroup container, int position, Object object) {
82        if (mCurTransaction == null) {
83            mCurTransaction = mFragmentManager.beginTransaction();
84        }
85        if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object
86                + " v=" + ((Fragment)object).getView());
87        mCurTransaction.detach((Fragment)object);
88    }
89
90    @Override
91    public void setPrimaryItem(ViewGroup container, int position, Object object) {
92        Fragment fragment = (Fragment)object;
93        if (fragment != mCurrentPrimaryItem) {
94            if (mCurrentPrimaryItem != null) {
95                mCurrentPrimaryItem.setMenuVisibility(false);
96                mCurrentPrimaryItem.setUserVisibleHint(false);
97            }
98            if (fragment != null) {
99                fragment.setMenuVisibility(true);
100                fragment.setUserVisibleHint(true);
101            }
102            mCurrentPrimaryItem = fragment;
103        }
104    }
105
106    @Override
107    public void finishUpdate(ViewGroup container) {
108        if (mCurTransaction != null) {
109            mCurTransaction.commitAllowingStateLoss();
110            mCurTransaction = null;
111            mFragmentManager.executePendingTransactions();
112        }
113    }
114
115    @Override
116    public boolean isViewFromObject(View view, Object object) {
117        return ((Fragment)object).getView() == view;
118    }
119
120    @Override
121    public Parcelable saveState() {
122        return null;
123    }
124
125    @Override
126    public void restoreState(Parcelable state, ClassLoader loader) {
127    }
128
129    private static String makeFragmentName(int viewId, int index) {
130        return "android:switcher:" + viewId + ":" + index;
131    }
132}
133