Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it crucial for performance to have ViewHolder as static in a ViewHolder pattern?

Is it crucial for performance to have ViewHolder as static in a ViewHolder pattern?

A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly. First, you need to create a class to hold your exact set of views. For example:

static class ViewHolder {
  TextView text;
  TextView timestamp;
  ImageView icon;
  ProgressBar progress;
  int position;
}
like image 206
Eugene Avatar asked Sep 11 '12 11:09

Eugene


People also ask

How does a ViewHolder improve performance?

ViewHolder is a design pattern which can be applied as a way around repeated use of findViewById() . A ViewHolder holds the reference to the id of the view resource and calls to the resource will not be required after you “find” them: Thus performance of the application increases.

What is ViewHolder pattern?

The ViewHolder design pattern enables you to access each list item view without the need for the look up, saving valuable processor cycles. Specifically, it avoids frequent call of findViewById() during ListView scrolling, and that will make it smooth.

What is the benefit of ViewHolder pattern in Android?

ViewHolder design pattern is used to speed up rendering of your ListView - actually to make it work smoothly. Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance.

When using the RecyclerView which design pattern helps prevent frequent findViewById () calls?

ViewHolder Pattern will prevents findViewById() to being called lots of times uselessy, keeping the views on a static reference, it is a good pattern to save some resources (expecially when you need to reference many views in your listview items).


2 Answers

Edit: misunderstood the question -- it seems you are asking specifically about making it static. That shouldn't be crucial for performance, but the idea is every bit helps.

Final edit here: Static nested class in Java, why?

====

Orig answer below:

It's very nice to squeeze performance out of a heavy ListView (or some other type of recycled AdapterView). However the best way to tell would be to profile the performance somehow.

Also at Google IO 2010 they recommend this method:

http://www.youtube.com/watch?v=wDBM6wVEO70

Edit:

Also here's a link to traceview to profile the performance, though I'm unsure how well it works.

http://developer.android.com/tools/debugging/debugging-tracing.html

like image 22
pjco Avatar answered Oct 01 '22 18:10

pjco


It's not crucial for performance, it is about using. If ViewHolder class will not be static - you have to provide instance of parent class:

No enclosing instance of type Type is accessible. 
Must qualify the allocation with an enclosing instance of type Type 
(e.g. x.new A() where x is an instance of Type).
like image 170
Jin35 Avatar answered Oct 01 '22 18:10

Jin35