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