Is it crucial for performance to have ViewHolder a

2020-06-09 04:32发布

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;
}

3条回答
等我变得足够好
2楼-- · 2020-06-09 05:10

It's not mandatory to do that. But when you use to do like this, you are using the views again when your adapter view is null. You are creating a view and assigning values to the view part, and tag the whole view using static class ViewHolder. So when you come back and view is not null, then visible part will come from to get the tag. This is how you will create less object as well less work load on adapter.

查看更多
Evening l夕情丶
3楼-- · 2020-06-09 05:12

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).
查看更多
孤傲高冷的网名
4楼-- · 2020-06-09 05:16

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

查看更多
登录 后发表回答