在列表视图中回收视图,值得吗? [英] Recycling views in a listview, worth it?

查看:27
本文介绍了在列表视图中回收视图,值得吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在android listview上覆盖baseadapter时,你必须实现这个方法public View getView(int position, View convertView, ViewGroup parent).convertview 是之前滚动时从列表中移出的视图,它是为了您可以重用该视图而不是创建新视图.

When overriding the baseadapter on an android listview, you have to implement this method public View getView(int position, View convertView, ViewGroup parent). The convertview is the view that was previously pushed off the list when scrolling, and it's given so that you can reuse that view instead of creating a new view.

我的问题是,真的有必要重用视图吗?如果只更改了一部分数据,我可以理解重用它.但是创建视图的开销真的有那么重要吗?我见过的每个关于使用列表视图的教程都告诉你要回收视图,即使是像文本视图这样简单的视图.

My question is, is it really necessary to reuse the view? I can understand reusing it if only a piece of the data is changed. But is the overhead of creating a view really THAT significant? Every tutorial on using listviews I've seen tells you to recycle the view, even on trivially simple views like a textview.

我想我的问题是为什么谷歌决定将其设为 getView 方法的默认行为?

I guess my question is why did google decide to make this the default behavior of the getView method?

推荐答案

回收视图的几个原因:

  • 创建对象的成本相对较高.创建的每个额外对象都需要由垃圾收集系统处理,并且至少会暂时增加您的内存占用
  • 这对于更复杂的视图更为重要,但膨胀和布局视图对象可能会很昂贵.大多数情况下,您只对 getView 中的视图进行细微更改,不会影响布局(例如,设置文本),因此您可能能够避免布局开销.
  • 请记住,Android 旨在在资源受限的环境中运行.
  • 最后,它已经为您完成了,它肯定不会伤害任何东西,所以为什么不使用它?

这篇关于在列表视图中回收视图,值得吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆