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