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.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 } 74 75 return fragment; 76 } 77 78 @Override 79 public void destroyItem(View container, int position, Object object) { 80 if (mCurTransaction == null) { 81 mCurTransaction = mFragmentManager.beginTransaction(); 82 } 83 if (DEBUG) Log.v(TAG, "Detaching item #" + position + ": f=" + object 84 + " v=" + ((Fragment)object).getView()); 85 mCurTransaction.detach((Fragment)object); 86 } 87 88 @Override 89 public void setPrimaryItem(View container, int position, Object object) { 90 Fragment fragment = (Fragment)object; 91 if (fragment != mCurrentPrimaryItem) { 92 if (mCurrentPrimaryItem != null) { 93 FragmentCompat.setMenuVisibility(mCurrentPrimaryItem, false); 94 } 95 if (fragment != null) { 96 FragmentCompat.setMenuVisibility(fragment, true); 97 } 98 mCurrentPrimaryItem = fragment; 99 } 100 } 101 102 @Override 103 public void finishUpdate(View container) { 104 if (mCurTransaction != null) { 105 mCurTransaction.commitAllowingStateLoss(); 106 mCurTransaction = null; 107 mFragmentManager.executePendingTransactions(); 108 } 109 } 110 111 @Override 112 public boolean isViewFromObject(View view, Object object) { 113 return ((Fragment)object).getView() == view; 114 } 115 116 @Override 117 public Parcelable saveState() { 118 return null; 119 } 120 121 @Override 122 public void restoreState(Parcelable state, ClassLoader loader) { 123 } 124 125 private static String makeFragmentName(int viewId, int index) { 126 return "android:switcher:" + viewId + ":" + index; 127 } 128}