Tagadapter

View recycling in Gallery

Did you know that view recycling in Gallery is not implemented?

https://groups.google.com/forum/#!topic/android-developers/mpvkI_za25g

“This is a known “bug” (it’s not a bug really, we just never had the
time to implement recycling in Gallery) but we have no ETA for a fix.” (c) Romain Guy

Custom solution can be found here http://pastebin.com/FWyYTt4D (http://goo.gl/lm6st)

//DL

HowTo: ListView, Adapter, getView and different list items’ layouts in one ListView

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:

  1. ListView asks adapter “give me a view” (getView) for each item of the list
  2. 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.

  1. If you have 1 billion items – there are only visible items in the memory + view in recycler.
  2. 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.
  3. 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:

Continue reading

© 2017 Android Tales

Theme by Anders NorenUp ↑