问题描述
在Android中的文章提高性能的多线程中开发人员博客,Adapter.getItem()
中使用convertView
来显示ImageView
的列表,这些列表是通过HttpRequest下载的.在Adapter.getItem()
中查看.
我想知道使用convertView
有什么优势?如何回收使用过的视图?已使用的视图缓存在哪里?
我问这个问题是因为我没有在项目中使用convertView
,我想知道不使用它的成本.下面是我对图像的listView的实现.
我没有使用convertView
,而是在Adapter.getItem()
中为新视图充气.此外,我创建了一个包装器类以将职员保存在listView
的每个项目中.下载图像后,它将作为位图存储在每个列表项的包装器类中(图像很小).这样,我可以避免重复下载相同的图像.而且,这也避免了多线程提高性能中讨论的竞争条件问题.一个>.但是我还是有点担心,是否有使用我的方法不好的问题?
仅供参考:@Carl Anderson推荐的文章详细介绍了convertView在适配器中的工作方式.本文建议的Romain Guy的Google IO是另一个很好的参考.
简而言之,使用convertView可以同时优化空间和时间.此外,我放弃了自己的imageDownloader并使用@CommonsWare推荐的Picasso.它像一种魅力.不重复使用convertView
的问题在于,正如您所说,每次创建和填充新的View
用户将一行滚动到视图中.与重用现有视图相比,创建和扩展视图要花费大量时间,并且您肯定为此付出了性能上的损失.
我的应用程序执行类似的操作-它在ListView中的行内使用ImageViews,并且这些ImageViews由在后台下载的图像填充.作为对线程错误的调查的一部分,我关闭了对Views的重用,并且这样做时的性能绝对糟糕. ListView代码针对视图重用进行了优化,如果您不赞成这样做,则会影响帧速率和可用性.
阅读此链接还帮助我更好地了解ListViews的工作方式: http://lucasr.org/2012/04/05/Performance-tips-for-androids-listview/
In the article Multithreading For Performance from Android Developer Blog, convertView
is used in Adapter.getItem()
to display a list of ImageView
, which are downloaded through HttpRequest.Yet, I also see some Android tutorials that don't use the convertView
, and just inflate a new view in Adapter.getItem()
.
I'm wondering what's the advantage of using convertView
? How does it recycle the used view? Where are the used view cached?
I ask this question because I didn't use convertView
in my project and I want to know the cost for not using it. Below is my implementation of a listView of images.
Instead of using convertView
, I inflate a new view in Adapter.getItem()
. Besides, I create a wrapper class to hold the staff in each item of listView
. Once the image is downloaded, it will be stored as bitmap in the wrapper class for each list item(The image is small). In this way, I can avoid the duplicate downloading of the same image. And this also avoid the race condition issues talked in Multithreading For Performance. But I'm still a little worried, is there any issues that not good by using my method?
FYI: the article recommended by @Carl Anderson gives details about how convertView works in adapter. The Google IO by Romain Guy that is suggested in this article is another good reference.
In a word, using convertView is both space and time optimized. Besides, I've abandoned my own imageDownloader and use the Picasso that is recommended by @CommonsWare. It works like a charm.
The problem with not re-using the convertView
is that, as you said, you are creating and inflating a new View
each and every time the user scrolls a row into view. Creating and inflating a view is a huge amount of time compared to reusing existing views, and you are certainly paying a performance hit for it.
My app does something similar - it uses ImageViews inside of rows in a ListView, and those ImageViews are populated by images that are downloaded in the background. As part of my investigation into a threading bug, I turned off reuse of Views, and the performance was absolutely terrible when I did that. ListView code is optimized for view reuse, and if you don't hold up to that, your framerate and usability will suffer.
Reading this link also helped me understand a lot better how ListViews work:http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
这篇关于Android convertView,是否要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!