http://blog.sina.com.cn/s/blog_5da93c8f0102ux6z.html
我们知道ListView加载大数据时,如果不做处理,会显示不流畅。通常情况下代码中设置ViewHolder缓存列表项来减少ListView的重复创建耗时,已达到ListView滚动时显示流程的效果。
但是如果ListView中的列表项结构复杂或者显示大量图片时,上述方法很可能造成ListView占用内存过多。因为缓存的列表项导致很多资源(比如图片)没不能被释放掉。
我们可以为列表设置一个回收监听器,来干预ListView的内存回收。
theListView.setRecyclerListener(new RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
// Cast the view to the type of the view we inflated.
MyCustomListRow row = (MyCustomListRow) view;
// Get the view that holds the image and nullify the drawable.
// GC will take care of cleaning up the memory used.
row.getThatImageDrawableView
}
});
//源码如下
/**
* Sets the recycler listener to be notified whenever a View is set aside in
* the recycler for later reuse. This listener can be used to free resources
* associated to the View.
*
* @param listener The recycler listener to be notified of views set aside
* in the recycler.
*
* @see android.widget.AbsListView.RecycleBin
* @see android.widget.AbsListView.RecyclerListener
*/
public void setRecyclerListener(RecyclerListener listener) {
mRecycler.mRecyclerListener = listener;
}