Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Wrap_Content fire BindView more than once

I am working on an Android app with a listView and am in the process of optimizing it. It uses a custom cursor adapter in one activity and I noticed that bindview() was firing twice for each row of the list. While researching the BindView() and NewView() methods here, I read in a post that having Wrap_Content for the width of my listview was a bad idea. I switched it to Fill_Parent and viola now bindview() and newview() each only fire once for each item.

So that I can better understand the internals of the Andorid OS, I would like to know why Wrap_Content caused bindview() to fire multiple times.

I have done several searches on Google, the android developer docs and here with no luck.

Any response is really appreciated.

Thanks, George

like image 844
George Baker Avatar asked Jul 03 '11 20:07

George Baker


1 Answers

If I recall correctly from a video from Google I/O, setting the width (or height) of a ListView to wrap_content will cause it to measure the first 3 items and base its dimensions off of those. In order to measure those first 3 items, it must call bindView() to populate them. Once it has measured, it will populate fully and call bindView() again on those first 3 and any subsequent.

This is due to the fact that the ListView only keeps in memory what is on the screen when you may have potentially an infinite number of items in the list. It is not designed to wrap_content to your largest item because it could be stuck in the drawing phase forever.

like image 121
Glendon Trullinger Avatar answered Oct 27 '22 03:10

Glendon Trullinger