How do I implement view recycling mechanism for PagerAdapter?

梦想与她 提交于 2019-11-28 07:16:27

So I have figured it out.

  1. overwrite destroyItem(ViewGroup container, int position, Object view) ans save you cached view
  2. create a separate method to see if there is any chance to use a recycled view or should you inflate a new one.
  3. remember to remove the recycled view from cache once its been used to avoid having same view attaching same view to the pager.

here is the code.. I used a Stack of view to cache all removed views from my pager

private View inflateOrRecycleView(Context context)
{

    View viewToReturn;
    mInflater = LayoutInflater.from(context);
    if (mRecycledViewsList.isEmpty())
    {
        viewToReturn = mInflater.inflate(R.layout.fragment_dates_grid_page, null, false);
    }
    else
    {
        viewToReturn = mRecycledViewsList.pop();
        Log.i(TAG,"Restored recycled view from cache "+ viewToReturn.hashCode());
    }


    return viewToReturn;
}

@Override
public void destroyItem(ViewGroup container, int position, Object view)
{
    VerticalViewPager pager = (VerticalViewPager) container;
    View recycledView = (View) view;
    pager.removeView(recycledView);
    mRecycledViewsList.push(recycledView);
    Log.i(TAG,"Stored view in cache "+ recycledView.hashCode());
}

do not forget to instantiate the stack in the adapter constructor.

i did it like this ..first create abstract softCache class:

public abstract class SoftCache<T> {
    private Stack<Reference<T>> mRecyclingStack;
    final Class<T> classType;

    public SoftCache(Class<T> typeParameterClass) {
        this.classType = typeParameterClass;
        mRecyclingStack = new Stack<Reference<T>>();
    }

    /* implement this to create new object of type T if cache is empty */
    public abstract T runWhenCacheEmpty();

    /*
     * retrieves last item from cache or creates a new T object if cache is
     * empty
     */
    public T get() {
        T itemCached = null;

        if (mRecyclingStack.isEmpty()) {
            itemCached = runWhenCacheEmpty();

        } else {
            SoftReference<T> softRef = (SoftReference<T>) mRecyclingStack
                    .pop();

            Object obj = softRef.get();
            /*
             * if referent object is empty(due to GC) then create a new
             * object
             */
            if (obj == null) {
                itemCached = runWhenCacheEmpty();

            }
            /*
             * otherwise restore from cache by casting the referent as the
             * class Type that was passed to constructor
             */
            else {
                itemCached = (classType.cast(softRef.get()));
            }
        }
        return itemCached;
    }

now inherit from SoftCache so we can implement the runWhenCacheEmpty method:

public class ViewCache extends SoftCache<View>{

            public ViewCache(Class<View> typeParameterClass) {
                super(typeParameterClass);
            }

            @Override
            public View runWhenCacheEmpty() {   
                return mFragment.getActivity().getLayoutInflater()
                        .inflate(R.layout.mypagelayout, null);
            }   
        }

then in your constructor instantiate it like this if you want it to be for a View class for example (but it can work for any type of class):

 SoftCache<View> myViewCache = new ViewCache(View.class);

now in destroyItem save the view to the cache:

@Override
    public void destroyItem(final ViewGroup container, final int position, final Object object) {
        final View v = (View) object;

        if(v.getId() == R.id.mypagelayout)
            myViewCache.put(v);  //this saves it


   }

now method instantiateItem make use of it simply like this:

@Override
    public Object instantiateItem(final ViewGroup container, final int position) {
       View MyPageView=myViewCache.get();

}

update: if you want to use the cache for different layouts or dont like to extend it i came up with a solution where you can use the same cache for multiple layouts where you would retrieve the layout you put in using the layouts ID:

public class SoftViewCache {

    private HashMap<Integer,ArrayList<SoftReference<View>>> multiMap;

    public SoftViewCache() {
        multiMap= new HashMap<Integer, ArrayList<SoftReference<View>>>();           
    }

    /*
     * retrieves cached item  or return null if cache is
     * empty
     */
    public View get(int id) {
        View itemCached = null;
        if (!multiMap.containsKey(id)) {
            return null;
        } 
        else {
            /*get the referent object and check if its already been GC if not we re-use*/
            SoftReference<View> softRef =multiMap.get(id).get(0);
            Object obj = softRef.get();
            /*
             * if referent object is empty(due to GC) then caller must create a new
             * object
             */
            if (null == obj) {
                return null;
            }
            /*
             * otherwise restore from cache 
             */
            else {
                itemCached = (softRef.get());
            }
        }
        return itemCached;
    }

    /* saves a view object to the cache by reference, we use a multiMap to allow
     * duplicate IDs*/
    public void put(View item) {
        SoftReference<View> ref = new SoftReference<View>(item);
        int key = item.getId();
          /*check if we already have a reuseable layouts saved if so just add to the list
           * of reusable layouts*/
        if (multiMap.containsKey(key)) {
            multiMap.get(key).add(ref);
        } else {
            /*otherwise we have no reusable layouts lets create a list of reusable layouts
             * and add it to the multiMap*/
            ArrayList<SoftReference<View>> list = new ArrayList<SoftReference<View>>();
            list.add(ref);
            multiMap.put(key, list);
        }
    }
}

I solved this by defining a RecycleCache, like this

protected static class RecycleCache {

  private final RecyclerPagerAdapter mAdapter;

  private final ViewGroup mParent;

  private final int mViewType;

  private List<ViewHolder> mCaches;

  public RecycleCache(RecyclerPagerAdapter adapter, ViewGroup parent, int viewType) {
    mAdapter = adapter;
    mParent = parent;
    mViewType = viewType;
    mCaches = new ArrayList<>();
  }

  public ViewHolder getFreeViewHolder() {
    int i = 0;
    ViewHolder viewHolder;
    for (int n = mCaches.size(); i < n; i++) {
      viewHolder = mCaches.get(i);
      if (!viewHolder.mIsAttached) {
        return viewHolder;
      }
    }
    viewHolder = mAdapter.onCreateViewHolder(mParent, mViewType);
    mCaches.add(viewHolder);
    return viewHolder;
  }
}

Check out my sample code here RecyclerPagerAdapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!