Holder class in adapter class

There is one simple question, which after searching I wanted to ask, why do we create a static holder class and assign it inside? Please clear my doubts, this will be a big help for me.

+8
android adapter
source share
3 answers

Your code can often call findViewById() while scrolling through a ListView , which can slow performance. Even when the adapter returns an inflated view for recycling, you still need to look for items and update them. The way to reuse findViewById() is to use the "view holder" template.

An object

A ViewHolder stores each of the component views inside the layout tag field, so you can immediately access them without having to re-review them. First, you need to create a class to hold your exact set of views.

Learn more about the Android Guideline .

  • Therefore, when you use the "View Holder", you can easily access each view without the need for a search, which saves valuable processor cycles.
  • with static inner class for views significantly improves performance

You can also see why Android prefers static classes .

Another interesting link How ListView works , after reading this blog, the developer can clear the LisView logic, and why you need to implement an internal static class for listView.

+7
source share

The answer is also simple if you understand how AdapterView works.

An AdapterView is a view whose children are defined by Adapter . AdapterView (more precisely, a specific implementation, such as ListView ) contains more information than is shown at any given time. To optimize memory consumption as well as performance, Adapter typically reuses a View representing individual items. Thus, you have fewer View objects than the corresponding data.

ViewGroups objects can be a complex hierarchy of View or ViewGroups . So, if you want to find individual View objects from this hierarchy, you will need to depend on the findViewById() method, and this becomes expensive if the view hierarchy has several levels. Therefore, for simplicity (and also to increase performance), the View-Holder template is used, where the individual View objects that interest us are assigned to a static inner class.

For more information on the View-Holder template, you can refer to the Lars Vogel Android ListView and ListActivity - Tutorial .

+2
source share

This is the point of optimization. Without viewHolder, you will need to call findViewById methods each time. with viewHolder you only need to call them once.

Example with TextView and 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(...) 
+1
source share

All Articles