Home | History | Annotate | Download | only in app
      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 
     17 package androidx.legacy.app;
     18 
     19 import android.app.Fragment;
     20 import android.app.FragmentManager;
     21 import android.app.FragmentTransaction;
     22 import android.os.Parcelable;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 
     27 import androidx.fragment.app.FragmentStatePagerAdapter;
     28 import androidx.viewpager.widget.PagerAdapter;
     29 
     30 /**
     31  * Implementation of {@link PagerAdapter} that
     32  * represents each page as a {@link android.app.Fragment} that is persistently
     33  * kept in the fragment manager as long as the user can return to the page.
     34  *
     35  * <p>This version of the pager is best for use when there are a handful of
     36  * typically more static fragments to be paged through, such as a set of tabs.
     37  * The fragment of each page the user visits will be kept in memory, though its
     38  * view hierarchy may be destroyed when not visible.  This can result in using
     39  * a significant amount of memory since fragment instances can hold on to an
     40  * arbitrary amount of state.  For larger sets of pages, consider
     41  * {@link FragmentStatePagerAdapter}.
     42  *
     43  * <p>When using FragmentPagerAdapter the host ViewPager must have a
     44  * valid ID set.</p>
     45  *
     46  * <p>Subclasses only need to implement {@link #getItem(int)}
     47  * and {@link #getCount()} to have a working adapter.
     48  *
     49  * <p>Here is an example implementation of a pager containing fragments of
     50  * lists:
     51  *
     52  * {@sample frameworks/support/samples/Support13Demos/src/main/java/com/example/android/supportv13/app/FragmentPagerSupport.java
     53  *      complete}
     54  *
     55  * <p>The <code>R.layout.fragment_pager</code> resource of the top-level fragment is:
     56  *
     57  * {@sample frameworks/support/samples/Support13Demos/src/main/res/layout/fragment_pager.xml
     58  *      complete}
     59  *
     60  * <p>The <code>R.layout.fragment_pager_list</code> resource containing each
     61  * individual fragment's layout is:
     62  *
     63  * {@sample frameworks/support/samples/Support13Demos/src/main/res/layout/fragment_pager_list.xml
     64  *      complete}
     65  *
     66  * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
     67  */
     68 @Deprecated
     69 public abstract class FragmentPagerAdapter extends PagerAdapter {
     70     private static final String TAG = "FragmentPagerAdapter";
     71     private static final boolean DEBUG = false;
     72 
     73     private final FragmentManager mFragmentManager;
     74     private FragmentTransaction mCurTransaction = null;
     75     private Fragment mCurrentPrimaryItem = null;
     76 
     77     /**
     78      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
     79      */
     80     @Deprecated
     81     public FragmentPagerAdapter(FragmentManager fm) {
     82         mFragmentManager = fm;
     83     }
     84 
     85     /**
     86      * Return the Fragment associated with a specified position.
     87      *
     88      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
     89      */
     90     @Deprecated
     91     public abstract Fragment getItem(int position);
     92 
     93     /**
     94      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
     95      */
     96     @Deprecated
     97     @Override
     98     public void startUpdate(ViewGroup container) {
     99         if (container.getId() == View.NO_ID) {
    100             throw new IllegalStateException("ViewPager with adapter " + this
    101                     + " requires a view id");
    102         }
    103     }
    104 
    105     /**
    106      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
    107      */
    108     @Deprecated
    109     @SuppressWarnings("ReferenceEquality")
    110     @Override
    111     public Object instantiateItem(ViewGroup container, int position) {
    112         if (mCurTransaction == null) {
    113             mCurTransaction = mFragmentManager.beginTransaction();
    114         }
    115 
    116         final long itemId = getItemId(position);
    117 
    118         // Do we already have this fragment?
    119         String name = makeFragmentName(container.getId(), itemId);
    120         Fragment fragment = mFragmentManager.findFragmentByTag(name);
    121         if (fragment != null) {
    122             if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
    123             mCurTransaction.attach(fragment);
    124         } else {
    125             fragment = getItem(position);
    126             if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
    127             mCurTransaction.add(container.getId(), fragment,
    128                     makeFragmentName(container.getId(), itemId));
    129         }
    130         if (fragment != mCurrentPrimaryItem) {
    131             fragment.setMenuVisibility(false);
    132             FragmentCompat.setUserVisibleHint(fragment, false);
    133         }
    134 
    135         return fragment;
    136     }
    137 
    138     /**
    139      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
    140      */
    141     @Deprecated
    142     @Override
    143     public void destroyItem(ViewGroup container, int position, Object object) {
    144         if (mCurTransaction == null) {
    145             mCurTransaction = mFragmentManager.beginTransaction();
    146         }
    147         if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
    148                 + " v=" + ((Fragment)object).getView());
    149         mCurTransaction.detach((Fragment)object);
    150     }
    151 
    152     /**
    153      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
    154      */
    155     @Deprecated
    156     @SuppressWarnings("ReferenceEquality")
    157     @Override
    158     public void setPrimaryItem(ViewGroup container, int position, Object object) {
    159         Fragment fragment = (Fragment)object;
    160         if (fragment != mCurrentPrimaryItem) {
    161             if (mCurrentPrimaryItem != null) {
    162                 mCurrentPrimaryItem.setMenuVisibility(false);
    163                 FragmentCompat.setUserVisibleHint(mCurrentPrimaryItem, false);
    164             }
    165             if (fragment != null) {
    166                 fragment.setMenuVisibility(true);
    167                 FragmentCompat.setUserVisibleHint(fragment, true);
    168             }
    169             mCurrentPrimaryItem = fragment;
    170         }
    171     }
    172 
    173     /**
    174      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
    175      */
    176     @Deprecated
    177     @Override
    178     public void finishUpdate(ViewGroup container) {
    179         if (mCurTransaction != null) {
    180             mCurTransaction.commitAllowingStateLoss();
    181             mCurTransaction = null;
    182             mFragmentManager.executePendingTransactions();
    183         }
    184     }
    185 
    186     /**
    187      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
    188      */
    189     @Deprecated
    190     @Override
    191     public boolean isViewFromObject(View view, Object object) {
    192         return ((Fragment)object).getView() == view;
    193     }
    194 
    195     /**
    196      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
    197      */
    198     @Deprecated
    199     @Override
    200     public Parcelable saveState() {
    201         return null;
    202     }
    203 
    204     /**
    205      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
    206      */
    207     @Deprecated
    208     @Override
    209     public void restoreState(Parcelable state, ClassLoader loader) {
    210     }
    211 
    212     /**
    213      * Return a unique identifier for the item at the given position.
    214      *
    215      * <p>The default implementation returns the given position.
    216      * Subclasses should override this method if the positions of items can change.</p>
    217      *
    218      * @param position Position within this adapter
    219      * @return Unique identifier for the item at position
    220      *
    221      * @deprecated Use {@link androidx.fragment.app.FragmentPagerAdapter} instead.
    222      */
    223     @Deprecated
    224     public long getItemId(int position) {
    225         return position;
    226     }
    227 
    228     private static String makeFragmentName(int viewId, long id) {
    229         return "android:switcher:" + viewId + ":" + id;
    230     }
    231 }