FragmentPagerAdapter.java revision 5c1637087453de15e31861f073eae5133c4e9f7b
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
36    public FragmentPagerAdapter(FragmentManager fm) {
37        mFragmentManager = fm;
38    }
39
40    /**
41     * Return the Fragment associated with a specified position.
42     */
43    public abstract Fragment getItem(int position);
44
45    @Override
46    public void startUpdate(View container) {
47    }
48
49    @Override
50    public Object instantiateItem(View container, int position) {
51        if (mCurTransaction == null) {
52            mCurTransaction = mFragmentManager.beginTransaction();
53        }
54
55        // Do we already have this fragment?
56        String name = makeFragmentName(container.getId(), position);
57        Fragment fragment = mFragmentManager.findFragmentByTag(name);
58        if (fragment != null) {
59            if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
60            mCurTransaction.attach(fragment);
61        } else {
62            fragment = getItem(position);
63            if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
64            mCurTransaction.add(container.getId(), fragment,
65                    makeFragmentName(container.getId(), position));
66        }
67
68        return fragment;
69    }
70
71    @Override
72    public void destroyItem(View container, int position, Object object) {
73        if (mCurTransaction == null) {
74            mCurTransaction = mFragmentManager.beginTransaction();
75        }
76        if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object
77                + " v=" + ((Fragment)object).getView());
78        mCurTransaction.detach((Fragment)object);
79    }
80
81    @Override
82    public void finishUpdate(View container) {
83        if (mCurTransaction != null) {
84            mCurTransaction.commit();
85            mCurTransaction = null;
86            mFragmentManager.executePendingTransactions();
87        }
88    }
89
90    @Override
91    public boolean isViewFromObject(View view, Object object) {
92        return ((Fragment)object).getView() == view;
93    }
94
95    @Override
96    public Parcelable saveState() {
97        return null;
98    }
99
100    @Override
101    public void restoreState(Parcelable state, ClassLoader loader) {
102    }
103
104    private static String makeFragmentName(int viewId, int index) {
105        return "android:switcher:" + viewId + ":" + index;
106    }
107}
108