Android ListView的优化

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






//源码如下


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值