3

When ViewHolder should use? I found this with most code for ListView and GridView. If anyone can explain. Thanks.

Maheshwar Ligade
  • 6,171
  • 3
  • 35
  • 53
Sagar Thakarar
  • 301
  • 3
  • 18
  • Read out both links [link1](http://developer.android.com/training/improving-layouts/smooth-scrolling.html) and [link2](http://stackoverflow.com/a/19289890/2183890) – Ahsan Kamal Jan 22 '16 at 05:12
  • Possible duplicate of [findViewById vs View Holder Pattern in ListView adapter](http://stackoverflow.com/questions/19289812/findviewbyid-vs-view-holder-pattern-in-listview-adapter) – RogueBaneling Jan 22 '16 at 05:21
  • Thanks. AKSiddique – Sagar Thakarar Jan 22 '16 at 05:22

2 Answers2

3

What’s with the ViewHolder pattern?

The ViewHolder design pattern enables you to access each list item view without the need for the look up, saving valuable processor cycles. Specifically, it avoids frequent call of findViewById() during ListView scrolling, and that will make it smooth.

Without the ViewHolder Design Pattern

Let’s take a look at our previous getView() method in ArrayAdapterItem.java

  1. The first time it was loaded, convertView is null. We’ll have to inflate our list item layout and find the TextView via findViewById().

  2. The second time it was loaded, convertView is not null, good! We don’t have to inflate it again. But we’ll use findViewById() again.

  3. The following times it was loaded, convertView is definitely not null. But findViewById() is constantly called, it will work but, it slows down the performance especially if you have lots of items and Views in your ListView.

With the ViewHolder Design Pattern

Now let’s see how it works with the ViewHolder pattern.

  1. The first time it was loaded, convertView is null. We’ll have to inflate our list item layout, instantiate the ViewHolder, find the TextView via findViewById() and assign it to the ViewHolder, and set the ViewHolder as tag of convertView.

  2. The second time it was loaded, convertView is not null, good! We don’t have to inflate it again. And here’s the sweet thing, we won’t have to call findViewById() since we can now access the TextView via its ViewHolder.

  3. The following time it was loaded, convertView is definitely not null. The findViewById() is never called again, and that makes our smooth ListView scrolling.

Go through Android Adapter, and ViewHolder Pattern for more detail with example.


Officially ,

WHY VIEWHOLDER ?

Because, Hold View Objects in a View Holder

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.

For example:

static class ViewHolder {
  TextView text;
  TextView timestamp;
  ImageView icon;
  ProgressBar progress;
  int position;
}

Then populate the ViewHolder and store it inside the layout.

ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);

Now you can easily access each view without the need for the look-up, saving valuable processor cycles.

Amit Vaghela
  • 21,317
  • 19
  • 79
  • 131
1

When ? If you want your listview to be scrolled smoothly and it should be responsive.

Why? When you user ViewHolder pattern , instead of creating custom row every time you scroll , you reuse the layout view which is hidden when scrolled down. Otherwise you will be all time inflating your custom lsitview layout row and it makes UI unresponsive and may show pop up dialog like Application not responding with ok and cancel option.

Krishna
  • 2,103
  • 14
  • 22