Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycling views in a listview, worth it?

When overriding the baseadapter on an android listview, you have to implement this method public View getView(int position, View convertView, ViewGroup parent). The convertview is the view that was previously pushed off the list when scrolling, and it's given so that you can reuse that view instead of creating a new view.

My question is, is it really necessary to reuse the view? I can understand reusing it if only a piece of the data is changed. But is the overhead of creating a view really THAT significant? Every tutorial on using listviews I've seen tells you to recycle the view, even on trivially simple views like a textview.

I guess my question is why did google decide to make this the default behavior of the getView method?

like image 770
Falmarri Avatar asked Sep 28 '10 22:09

Falmarri


2 Answers

A couple of reasons to recycle views:

  • Object creation is relatively expensive. Every additional object that is created needs to be dealt with by the garbage collection system, and at least temporarily increases your memory footprint
  • This is more important for more complex views, but inflating and laying out the view objects can be expensive. Most often, you are only making minor changes to the view in getView that won't affect the layout (e.g, setting text) so you might be able to avoid the layout overhead.
  • Remember that Android is designed to be run in a resource constrained environment.
  • Finally, its already done for you, and it certianly doesn't hurt anything, so why not use it?
like image 137
Cheryl Simon Avatar answered Oct 16 '22 20:10

Cheryl Simon


Is it necessary? Only if you like an extra 30-40 fps during flings on a Nexus One. :) (See the slides from http://code.google.com/events/io/2010/sessions/world-of-listview-android.html, slides 13-17)

Why make the device do work that it doesn't need to do by ignoring a significant optimization that's been 95% done for you?

like image 36
adamp Avatar answered Oct 16 '22 19:10

adamp