简体   繁体   English

ViewHolder类和OOM管理

[英]ViewHolder Class and OOM Management

Does using the ViewHolder class may prevent leaks and large heap retains? 使用ViewHolder类是否可以防止泄漏和大堆保留?

   class holder
    {
        WeakReference<ImageView> img;

    }

holder holdr = new holder();
holder.img = ...

displayImage(holder.img);

Should I just pass it as a parameter, Instead of declaring it as global? 我应该只是将其作为参数传递,而不是将其声明为全局参数吗?

The ViewHolder-Pattern got developed mainly due to performance-issues when you are using a ListAdapter that displays some View-elements with the same structure over and over for each list-item. 之所以开发ViewHolder-Pattern,主要是因为当您使用ListAdapter时,由于性能问题,ListAdapter会为每个列表项反复显示一些具有相同结构的View元素。

Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. 您的代码可能在滚动ListView期间频繁调用findViewById(),这可能会降低性能。 Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. 即使适配器返回膨胀视图以进行回收,您仍然需要查找元素并进行更新。 A way around repeated use of findViewById() is to use the "view holder" design pattern. 重复使用findViewById()的一种方法是使用“视图持有者”设计模式。

You should also make your ViewHolder-class static 您还应该将ViewHolder类设为静态

You can read more about it on Googles Dev Page . 您可以在Googles Dev Page上了解更多信息。

I also recommend watching this Google IO video 我还建议您观看此Google IO视频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM