Advertisement
Guest User

Adapted FragmentPageAdapter.java

a guest
Mar 4th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1. /*
  2.     Edited FragmentPageAdapter.java by Ivan Seidel | github.com/ivanseidel
  3.    
  4.     This was made to add the method 'clear', allowing it to force a clear on the caching of fragments.
  5.  
  6.     More can be found on my StackOverflow Thread:
  7.     http://stackoverflow.com/questions/16601048/viewpager-skip-views-on-return-leaving-blank/16603264
  8. */
  9. /*
  10.  * Copyright (C) 2011 The Android Open Source Project
  11.  *
  12.  * Licensed under the Apache License, Version 2.0 (the "License");
  13.  * you may not use this file except in compliance with the License.
  14.  * You may obtain a copy of the License at
  15.  *
  16.  *      http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software
  19.  * distributed under the License is distributed on an "AS IS" BASIS,
  20.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21.  * See the License for the specific language governing permissions and
  22.  * limitations under the License.
  23.  */
  24.  
  25.  
  26. import android.os.Parcelable;
  27. import android.support.v4.app.Fragment;
  28. import android.support.v4.app.FragmentManager;
  29. import android.support.v4.app.FragmentStatePagerAdapter;
  30. import android.support.v4.app.FragmentTransaction;
  31. import android.support.v4.view.PagerAdapter;
  32. import android.util.Log;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35.  
  36. /**
  37.  * Implementation of {@link android.support.v4.view.PagerAdapter} that
  38.  * represents each page as a {@link Fragment} that is persistently
  39.  * kept in the fragment manager as long as the user can return to the page.
  40.  *
  41.  * <p>This version of the pager is best for use when there are a handful of
  42.  * typically more static fragments to be paged through, such as a set of tabs.
  43.  * The fragment of each page the user visits will be kept in memory, though its
  44.  * view hierarchy may be destroyed when not visible.  This can result in using
  45.  * a significant amount of memory since fragment instances can hold on to an
  46.  * arbitrary amount of state.  For larger sets of pages, consider
  47.  * {@link FragmentStatePagerAdapter}.
  48.  *
  49.  * <p>When using FragmentPagerAdapter the host ViewPager must have a
  50.  * valid ID set.</p>
  51.  *
  52.  * <p>Subclasses only need to implement {@link #getItem(int)}
  53.  * and {@link #getCount()} to have a working adapter.
  54.  *
  55.  * <p>Here is an example implementation of a pager containing fragments of
  56.  * lists:
  57.  *
  58.  * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentPagerSupport.java
  59.  *      complete}
  60.  *
  61.  * <p>The <code>R.layout.fragment_pager</code> resource of the top-level fragment is:
  62.  *
  63.  * {@sample development/samples/Support4Demos/res/layout/fragment_pager.xml
  64.  *      complete}
  65.  *
  66.  * <p>The <code>R.layout.fragment_pager_list</code> resource containing each
  67.  * individual fragment's layout is:
  68.  *
  69.  * {@sample development/samples/Support4Demos/res/layout/fragment_pager_list.xml
  70.  *      complete}
  71.  */
  72. public abstract class FragmentPagerAdapter extends PagerAdapter {
  73.     private static final String TAG = "FragmentPagerAdapter";
  74.     private static final boolean DEBUG = false;
  75.  
  76.     private final FragmentManager mFragmentManager;
  77.     private FragmentTransaction mCurTransaction = null;
  78.     private Fragment mCurrentPrimaryItem = null;
  79.  
  80.     public FragmentPagerAdapter(FragmentManager fm) {
  81.         mFragmentManager = fm;
  82.     }
  83.  
  84.     public void clear(ViewGroup container){
  85.         if (mCurTransaction == null) {
  86.             mCurTransaction = mFragmentManager.beginTransaction();
  87.         }
  88.        
  89.         for(int i = 0; i < getCount(); i++){
  90.            
  91.             final long itemId = getItemId(i);
  92.  
  93.             // Do we already have this fragment?
  94.             String name = makeFragmentName(container.getId(), itemId);
  95.             Fragment fragment = mFragmentManager.findFragmentByTag(name);
  96.            
  97.             if(fragment != null){
  98.                 mCurTransaction.detach(fragment);
  99.             }
  100.         }
  101.         mCurTransaction.commitAllowingStateLoss();
  102.         mCurTransaction = null;
  103.     }
  104.    
  105.     /**
  106.      * Return the Fragment associated with a specified position.
  107.      */
  108.     public abstract Fragment getItem(int position);
  109.  
  110.     @Override
  111.     public void startUpdate(ViewGroup container) {
  112.     }
  113.  
  114.     @Override
  115.     public Object instantiateItem(ViewGroup container, int position) {
  116.         if (mCurTransaction == null) {
  117.             mCurTransaction = mFragmentManager.beginTransaction();
  118.         }
  119.  
  120.         final long itemId = getItemId(position);
  121.  
  122.         // Do we already have this fragment?
  123.         String name = makeFragmentName(container.getId(), itemId);
  124.         Fragment fragment = mFragmentManager.findFragmentByTag(name);
  125.         if (fragment != null) {
  126.             if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
  127.             mCurTransaction.attach(fragment);
  128.         } else {
  129.             fragment = getItem(position);
  130.             if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
  131.             mCurTransaction.add(container.getId(), fragment,
  132.                     makeFragmentName(container.getId(), itemId));
  133.         }
  134.         if (fragment != mCurrentPrimaryItem) {
  135.             fragment.setMenuVisibility(false);
  136.             fragment.setUserVisibleHint(false);
  137.         }
  138.  
  139.         return fragment;
  140.     }
  141.  
  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.     @Override
  153.     public void setPrimaryItem(ViewGroup container, int position, Object object) {
  154.         Fragment fragment = (Fragment)object;
  155.         if (fragment != mCurrentPrimaryItem) {
  156.             if (mCurrentPrimaryItem != null) {
  157.                 mCurrentPrimaryItem.setMenuVisibility(false);
  158.                 mCurrentPrimaryItem.setUserVisibleHint(false);
  159.             }
  160.             if (fragment != null) {
  161.                 fragment.setMenuVisibility(true);
  162.                 fragment.setUserVisibleHint(true);
  163.             }
  164.             mCurrentPrimaryItem = fragment;
  165.         }
  166.     }
  167.  
  168.     @Override
  169.     public void finishUpdate(ViewGroup container) {
  170.         if (mCurTransaction != null) {
  171.             mCurTransaction.commitAllowingStateLoss();
  172.             mCurTransaction = null;
  173.             mFragmentManager.executePendingTransactions();
  174.         }
  175.     }
  176.  
  177.     @Override
  178.     public boolean isViewFromObject(View view, Object object) {
  179.         return ((Fragment)object).getView() == view;
  180.     }
  181.  
  182.     @Override
  183.     public Parcelable saveState() {
  184.         return null;
  185.     }
  186.  
  187.     @Override
  188.     public void restoreState(Parcelable state, ClassLoader loader) {
  189.     }
  190.  
  191.     /**
  192.      * Return a unique identifier for the item at the given position.
  193.      *
  194.      * <p>The default implementation returns the given position.
  195.      * Subclasses should override this method if the positions of items can change.</p>
  196.      *
  197.      * @param position Position within this adapter
  198.      * @return Unique identifier for the item at position
  199.      */
  200.     public long getItemId(int position) {
  201.         return position;
  202.     }
  203.  
  204.     private static String makeFragmentName(int viewId, long id) {
  205.         return "android:switcher:" + viewId + ":" + id;
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement