FragmentPagerAdapter.java revision 2a4d8518f36346ea25a22a736453ff28f2954165
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;
23
24/**
25 * Implementation of {@link android.support.v4.view.PagerAdapter} that
26 * represents each page as a {@link Fragment} that is persistently
27 * kept in the fragment manager as long as the user can return to the page.
28 */
29public abstract class FragmentPagerAdapter extends PagerAdapter {
30    private static final String TAG = "FragmentPagerAdapter";
31    private static final boolean DEBUG = false;
32
33    private final FragmentManager mFragmentManager;
34    private FragmentTransaction mCurTransaction = null;
35    private Fragment mCurrentPrimaryItem = null;
36
37    public FragmentPagerAdapter(FragmentManager fm) {
38        mFragmentManager = fm;
39    }
40
41    /**
42     * Return the Fragment associated with a specified position.
43     */
44    public abstract Fragment getItem(int position);
45
46    @Override
47    public void startUpdate(View container) {
48    }
49
50    @Override
51    public Object instantiateItem(View container, int position) {
52        if (mCurTransaction == null) {
53            mCurTransaction = mFragmentManager.beginTransaction();
54        }
55
56        // Do we already have this fragment?
57        String name = makeFragmentName(container.getId(), position);
58        Fragment fragment = mFragmentManager.findFragmentByTag(name);
59        if (fragment != null) {
60            if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
61            mCurTransaction.attach(fragment);
62        } else {
63            fragment = getItem(position);
64            if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
65            mCurTransaction.add(container.getId(), fragment,
66                    makeFragmentName(container.getId(), position));
67        }
68        if (fragment != mCurrentPrimaryItem) {
69            fragment.setMenuVisibility(false);
70        }
71
72        return fragment;
73    }
74
75    @Override
76    public void destroyItem(View container, int position, Object object) {
77        if (mCurTransaction == null) {
78            mCurTransaction = mFragmentManager.beginTransaction();
79        }
80        if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object
81                + " v=" + ((Fragment)object).getView());
82        mCurTransaction.detach((Fragment)object);
83    }
84
85    @Override
86    public void setPrimaryItem(View container, int position, Object object) {
87        Fragment fragment = (Fragment)object;
88        if (fragment != mCurrentPrimaryItem) {
89            if (mCurrentPrimaryItem != null) {
90                mCurrentPrimaryItem.setMenuVisibility(false);
91            }
92            if (fragment != null) {
93                fragment.setMenuVisibility(true);
94            }
95            mCurrentPrimaryItem = fragment;
96        }
97    }
98
99    @Override
100    public void finishUpdate(View container) {
101        if (mCurTransaction != null) {
102            mCurTransaction.commitAllowingStateLoss();
103            mCurTransaction = null;
104            mFragmentManager.executePendingTransactions();
105        }
106    }
107
108    @Override
109    public boolean isViewFromObject(View view, Object object) {
110        return ((Fragment)object).getView() == view;
111    }
112
113    @Override
114    public Parcelable saveState() {
115        return null;
116    }
117
118    @Override
119    public void restoreState(Parcelable state, ClassLoader loader) {
120    }
121
122    private static String makeFragmentName(int viewId, int index) {
123        return "android:switcher:" + viewId + ":" + index;
124    }
125}
126