简体   繁体   中英

GetView method and convertView misconception?

I have read several times android documentation about the GetView DataAdapter class. From what I understand, if I have a homogeneus list where every item is just a textview and all items fall inside the screen (no scroll is possible) there won't be any view recycling and so I should receive 4 calls to the GetView with convertView being null. Well, this is not what happens to me. First time convertView is null, but it is the same for the others 3 created items. List is populated correctly but I would like to understand why is this happening. If anyone could help I would be really greateful.

I believe Android always tries to recycle views because that will make population fastest. It doesn't matter if all the views fit into the visible viewport of the list. Inflation/creation of views is very expensive, and that's why Android wants to recycle as much as possible.

You are guaranteed that the view passed into GetView will be of the same view type (defined in the data adapter) or null. If it's null, you need to create a new view for that view type, otherwise you should try to reuse the view that is passed in. For a homogeneous list of TextView's, it's very simple:

if (convertView == null)
{
    TextView tv = new TextView();
    ....
    tv.setText("First Item");
}
else
{
    TextView tv = (TextView) convertView;
    ...
    tv.setText("Recycled Item");
}

For simplicity sake, you could always return a new view instead of using the convertView, but performance wouldn't be as great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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