Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Holder Class in Adapter Class

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.

like image 399
user1537629 Avatar asked Jul 19 '12 11:07

user1537629


People also ask

What is Holder in RecyclerView?

A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results.

What is adapter and types?

In Android, Adapter is a bridge between UI component and data source that helps us to fill data in UI component. It holds the data and send the data to an Adapter view then view can takes the data from the adapter view and shows the data on different views like as ListView, GridView, Spinner etc.

What is the use of notifyDataSetChanged in Android?

notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.

Why is adapter class is used in Android?

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.


2 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.

  1. 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.
  2. 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.

like image 187
Vivek Kumar Srivastava Avatar answered Oct 05 '22 19:10

Vivek Kumar Srivastava


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 Views 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 Views 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.

like image 39
Rajesh Avatar answered Oct 05 '22 19:10

Rajesh