29

我有一个寻呼机适配器,它假设为表示日历的复杂视图充气。

每年膨胀日历大约需要 350 毫秒。

为了提高性能,我想实现与ListView回收视图的数组适配器中存在的相同机制(convertView参数 in getView())。

这是我getView()来自适配器的电流。

@Override
protected View getView(VerticalViewPager pager, final DateTileGrid currentDataItem, int position)
{
    mInflater = LayoutInflater.from(pager.getContext());

        // This is were i would like to understand weather is should use a recycled view or create a new one.
    View datesGridView = mInflater.inflate(R.layout.fragment_dates_grid_page, pager, false);


    DateTileGridView datesGrid = (DateTileGridView) datesGridView.findViewById(R.id.datesGridMainGrid);
    TextView yearTitle = (TextView) datesGridView.findViewById(R.id.datesGridYearTextView);
    yearTitle.setText(currentDataItem.getCurrentYear() + "");
    DateTileView[] tiles = datesGrid.getTiles();

    for (int i = 0; i < 12; i++)
    {
        String pictureCount = currentDataItem.getTile(i).getPictureCount().toString();
        tiles[i].setCenterLabel(pictureCount);
        final int finalI = i;
        tiles[i].setOnCheckedChangeListener(new DateTileView.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(DateTileView tileChecked, boolean isChecked)
            {
                DateTile tile = currentDataItem.getTile(finalI);
                tile.isSelected(isChecked);
            }
        });
    }

    return datesGridView;
}

实现这种行为的任何指示或方向?特别是我如何在适配器中知道其中一个DateTileGridViews正在从屏幕上滑动,以便我可以将其保存在内存中以供下次重用。

4

3 回答 3

32

所以我想通了。

  1. 覆盖destroyItem(ViewGroup container, int position, Object view)ans 为您保存缓存视图
  2. 创建一个单独的方法来查看是否有机会使用回收的视图,或者是否应该膨胀一个新的视图。
  3. 请记住,一旦使用了回收的视图,就从缓存中删除它,以避免将相同的视图附加到寻呼机上。

这是代码..我使用堆栈视图来缓存从我的寻呼机中删除的所有视图

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());
}

不要忘记在适配器构造函数中实例化堆栈。

于 2013-09-26T06:16:05.253 回答
3

我是这样做的..首先创建抽象的softCache类:

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;
    }

现在从 SoftCache 继承,所以我们可以实现 runWhenCacheEmpty 方法:

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);
            }   
        }

然后在您的构造函数中实例化它,如果您希望它用于 View 类,例如(但它可以用于任何类型的类):

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

现在在 destroyItem 中将视图保存到缓存中:

@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


   }

现在方法 instantiateItem 像这样使用它:

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

}

更新:如果您想将缓存用于不同的布局或不喜欢扩展它,我想出了一个解决方案,您可以将相同的缓存用于多个布局,您可以使用布局 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);
        }
    }
}
于 2014-08-06T19:59:45.840 回答
3

我通过定义 a 解决了这个问题RecycleCache,就像这样

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;
  }
}

在这里查看我的示例代码RecyclerPagerAdapter

于 2015-11-14T02:19:02.400 回答