Home » Android » android – Recycling views in a listview, worth it?

android – Recycling views in a listview, worth it?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&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?

Answer:

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?