1.RecyclerView非常灵活,包括定制RecycledViewPool。
参考接口:
public void setRecycledViewPool(RecycledViewPool pool)
示例:
final RecyclerView.RecycledViewPool pool = new RecyclerView.RecycledViewPool() {
@Override
public void putRecycledView(RecyclerView.ViewHolder scrap) {
super.putRecycledView(scrap);
int cnt = childCount.decrementAndGet();
if (DEBUG) {
Log.d(TAG, "CHILD_CNT(put):" + cnt + ", " + scrap);
}
}
@Override
public RecyclerView.ViewHolder getRecycledView(int viewType) {
final RecyclerView.ViewHolder recycledView = super.getRecycledView(viewType);
if (recycledView != null) {
final int cnt = childCount.incrementAndGet();
if (DEBUG) {
Log.d(TAG, "CHILD_CNT(get):" + cnt + ", " + recycledView);
}
}
return recycledView;
}
};
pool.setMaxRecycledViews(mTestAdapter.getItemViewType(0), 500);
mRecyclerView.setRecycledViewPool(pool);
指定缓存大小(DEFAULT_MAX_SCRAP = 5)
public void setMaxRecycledViews(int viewType, int max)
示例:
mRecyclerView.getRecycledViewPool().setMaxRecycledViews(mTestAdapter.getItemViewType(0), 500);
PS:默认情况下,应该很少发生OOM的,可能是使用的drawable过大。内存释放这块,目前可以参考Volley在NetworkImageView中重写onDetachedFromWindow做特殊处理setImageBitmap(null);但很快发现反复滑动后出现ImageView显示空白的问题(仔细观察会发现正好是两个)。
2.RecyclerView中有两种缓存,mCachedViews(DEFAULT_CACHE_SIZE = 2)、mRecyclerPool(DEFAULT_MAX_SCRAP = 5),有点像两级缓存,如果把一级缓存关掉即setItemViewCacheSize(0),理论上应该可以保证onBindViewHolder的执行,然后可以采用onDetachedFromWindow中销毁的方式解决OOM问题。