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.
Asked
Active
Viewed 1,016 times
3
-
i have tried to explain [here][1] how listview and recycling mechanism works [1]: http://stackoverflow.com/a/14108676/1939564 – Muhammad Babar Jan 02 '13 at 16:43
1 Answers
4
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.

Samuel
- 16,923
- 6
- 62
- 75
-
-
So, Why am I getting 4 different texts showing in the list? If it is allways recycling first created Wouldn't I be chaning text of original view and so, having a list with 4 items with a reference to the same view? I'm a bit messed up. – Notbad Aug 08 '12 at 15:07
-
The number of View references Android holds onto under its hood does not equal the number of items in the list. Once you return the View reference to Android, you shouldn't think about it anymore. Android will take care of the lifetime of the View reference. It probably just renders the View to a bitmap and then since it doesn't need the View object, it gives it back for recycling. Or maybe it makes a fast copy of the View that it holds of to for a while. Take a look at the Android source if you're curious about how it works. I bet it's very interesting. – Samuel Aug 08 '12 at 15:14
-
Thanks for the info. I will. Anyway this makes an idea I had initially non sense. I wanted to add an OnClick to every textview in every list item, this seems not to work correctly because of the reusing. – Notbad Aug 08 '12 at 15:21
-
1@Notbad you can still add an `onClickListener()` to them. The easiest way is to use `ListView.setOnItemClickListener()` but you can also do it by setting a listener on each TextView before you return it inside the getView() method. – FoamyGuy Aug 08 '12 at 15:30
-
1@Notbad, Tim is correct. That should work with the recycling. If you post your code in this question we could look at it and perhaps diagnose what the issue is. – Samuel Aug 08 '12 at 15:33
-
I would really like to add the code but I can't because NDA. Anyway gonna do some tests with your suggestions. Thanks. – Notbad Aug 08 '12 at 15:39
-
Android will recycle items when some items of your listview go outside of the screen, if you have only 4 items, and all of them fit on the screen, android won't recycle any views because it doesn't have to – Julian Suarez Aug 08 '12 at 15:52