Android convertView, to use it or not?
Asked Answered
A

4

5

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.

Athalee answered 29/4, 2014 at 21:53 Comment(0)
N
7

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/

Natality answered 29/4, 2014 at 22:1 Comment(0)
R
2

I also see some Android tutorials that don't use the convertView, and just inflate a new view in Adapter.getItem().

That is not a good sign.

I'm wondering what's the advantage of using convertView?

It saves CPU time and generates less garbage in the heap.

How does it recycle the used view?

It is the used view.

Where are the used view cached?

In AdapterView.

I ask this question because I didn't use convertView in my project and I want to know the cost for not using it.

Well, depending on how you wrote your adapter, you might be getting row recycling "for free" from your superclass.

Below is my implementation of a listView of images.

There is no code in your question.

Instead of using convertView, I inflate a new view in Adapter.getItem().

Only do that if convertView is null. Otherwise, use convertView. This takes one if/else construct.

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.

I do not quite understand where/how you are downloading the image. There are many, many libraries that do this for you, and using one is usually a better idea than is rolling your own code. I like Picasso and Ion for this, but there are others.

But I'm still a little worried, is there any issues that not good by using my method?

There may be more than what I have listed, but without seeing any code, it is difficult to guess what may be not good.

Relegate answered 29/4, 2014 at 21:59 Comment(0)
V
1

The convertView item is just a helper view, although a very important one, that helps you create less Views.

ConvertView is an optimization that has less to do with image downloading, than with regular view creation. Inflating views in android is an expensive operation, and if your list has many items, and particularly more items that can fit on one screen, the device will have to create several almost identical views to populate your listView.

Instead when you use convertView you help the system create less views, because it gives you the ability to reuse views that the system already created but that are not visible to the user, so you just need to change its contents instead of creating a new view for every item.

Not using convertView is a common cause for lag when scrolling your listviews

Vonvona answered 29/4, 2014 at 22:2 Comment(0)
I
0

Using convertView in the most basic way is simply for recycling views rather than creating new views each time the view is needed, hence if your listview never losses view you might not need to implement it eg a short listview of about 3-4 views. But it is good practice check this google I/o presentation

Intercommunion answered 29/4, 2014 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.