There is one simple question which after searching i wanted to ask that why we create static holder class and assign the views inside it?? Please clear my doubts it will be great help to me.
-
Is your question regarding 'static' qualifier, or ViewHolder in general? Have you read the documentation: http://developer.android.com/training/improving-layouts/smooth-scrolling.html – IgorGanapolsky Mar 03 '14 at 20:55
3 Answers
Your code might call findViewById()
frequently during the scrolling of ListView
, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById()
is to use the "view holder"
design 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.
You can read Android Guideline for more detail.
- So when you are using "View Holder" then you can easily access each view without the need for the look-up which saving valuable processor cycles.
- having a
static inner class
for the views greatly improves performance
you can also see Why does Android prefer static classes link.
One more interesting link How ListView Work, after reading this blog developer can clear logic of LisView and also why need to implement inner static class for listView.

- 1
- 1

- 2,158
- 1
- 16
- 23
The answer is also simple if you understand how an AdapterView
works.
An AdapterView
is a view whose children are determined by an Adapter
. The AdapterView
(more specifically the concrete implementaion like ListView
) contains more information than what is shown at any given time. To optimize the memory consumption and also for the sake of performance, the Adapter
usually reuses the View
s representing the individual items. So you have less number of View
objects than the corresponding data.
The reused objects can be a complex hierarchy of View
s or ViewGroups
. So, in case you want to find the individual View
objects from within this hierarchy, you would need to depend on findViewById()
method and this becomes costly when the view hierarchy has multiple levels. So for simplicity (and also to improve performance), the View-Holder pattern is used, where the individual View
objects that we are interested in are assigned to a static inner class.
You can refer to Lars Vogel's Android ListView and ListActivity - Tutorial for more information about the View-Holder pattern.

- 15,724
- 7
- 46
- 95
It's an optimization point. Without a viewHolder, you'll need to call the findViewById methods each time. with the viewHolder, you only need to call these once.
Example with a TextView and an ImageView:
Without ViewHolder:
if (convertView == null) { convertView = mInflater.inflate(..., null); } //Following called each time TextView tv = (TextView)convertView.findViewById(...); ImageView iv = (ImageView)convertView.findViewById(...); tv.setText(...)
With ViewHolder:
if (convertView == null) { convertView = mInflater.inflate(..., null); holder = new ViewHolder(); //called once holder.tv = (TextView) vi.findViewById(..); holder.iv = (ImageView) vi.findViewById(...); vi.setTag(holder); } else { holder = (ViewHolder) vi.getTag(); } holder.tv.setText(...)

- 4,354
- 1
- 28
- 43