I was surprised that getViewTypeCount() is so rarely overrided (codesearch). If you are an expert in this – this post is not for you:-)
ListView and Adapter Basics
How it works:
- ListView asks adapter “give me a view” (getView) for each item of the list
- A new View is returned and displayed
Next question – what if we have one billion items? Create new view for each item? The answer is NO:-) Android caches views for you.
There’s a component in Android called “Recycler”. I drawed a picture based on Romain Guy presentation at Google IO ’09.
- If you have 1 billion items – there are only visible items in the memory + view in recycler.
- ListView asks for a view type1 first time (getView) x visible items. convertView is null in getView – you create new view of type1 and return it.
- ListView asks for a view type1 when one item1 is outside of the window and new item the same type is comming from the bottom. convertView is not null = item1. You should just set new data and return convertView back. No need to create view again.
Let’s write a simple code and put System.out to the getView: