你可能知道要使用缓存convertView,这样不用每次都是拿到布局的xml文件,但是当要设置布局中的某一个组件的时候,你还是要用到findViewById,所以当你在滑动listview时候,findViewById就会被频繁的调用,这样就会影响到你的显示,所以这个时候就要用到ViewHolder,不但将布局对象缓存起来,并且布局里面的要使用的组件也缓存起来,这样占用内存就少了,listview得到了优化。
下面是官方的解释:
Your code might call findViewById()
frequently during the scrolling of ListView
, which can slow down performance. 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.