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.
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.
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.
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.
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.
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.
static inner class
for the views greatly improves performanceyou 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With