1

I'm trying to learn Android programming. And I can't find an explication to this algorithm :

public View getView(int r, View convertView, ViewGroup parent) {
  ViewHolder holder = null;
  // Si la vue n'est pas recyclée
  if(convertView == null) {
    // On récupère le layout
    convertView  = mInflater.inflate(R.layout.item, null);

    holder = new ViewHolder();
    // On place les widgets de notre layout dans le holder
    holder.mNom = (TextView) convertView.findViewById(R.id.nom);
    holder.mNumero = (TextView) convertView.findViewById(R.id.numero);
    holder.mPhoto = (ImageView) convertView.findViewById(R.id.photo);

    // puis on insère le holder en tant que tag dans le layout
    convertView.setTag(holder);
  } else {
    // Si on recycle la vue, on récupère son holder en tag
    holder = (ViewHolder)convertView.getTag();
  }

  // Dans tous les cas, on récupère le contact téléphonique concerné
  Contact c = (Contact)getItem(r);
  // Si cet élément existe vraiment…
  if(c != null) {
    // On place dans le holder les informations sur le contact
    holder.mNom.setText(c.getNom());
    holder.mNumero.setText(c.getNumero());
  }
  return convertView;
}
  • Why dos the writer checked if convertView is null and if it's not ?
  • What's the need of the ViewHolder pattern in this contexte, please ?

Can someone help me to understand it please ?

Thanks in advance.

rzqr
  • 259
  • 2
  • 3
  • 10

2 Answers2

4

ListView tries to recycle the "row" views that are not already in use. As you are scrolling down, some rows disappear from the top of the screen. Those views are passed down to your adapter as the convertView parameter as they become available so that you can reuse them to build new rows instead of inflating new ones. So you should always try to inflate new views only when the convertView passed down is null, or in other words, when you don't have a previously inflated view to recycle.

The ViewHolder pattern tries to avoid successive calls to findViewById (which is an expensive operation) in those recycled views. Instead of looking for all the view references that you need every time you have to generate a new row, you do it only once, just after it has been inflated, store those references in a 'ViewHolder' object, and store it as a tag for the current view. When that view returns to you as a convertView after it has been recycled, you know you can just retrieve the ViewHolder object that you saved previously and all your references will be there. No need to look for them again.

ivagarz
  • 2,434
  • 22
  • 22
  • When I scroll down the ListView to see other elements why can take sometimes convertView a null value ? Doesn't it update the convertView automatically ? – rzqr Sep 28 '13 at 17:32
  • Yes, ListView will hand you an appropriate convertView automatically as it becomes available. There a number of reasons that can cause the convertView to be null. That will obviously be the case when the list is first populated and all the rows must be created new. If you have multiple row types, it is possible that a new type shows up and you don't have any previous row to recycle. Some processes, like running animations, can cause a view to not be fit for recycling, and force you to inflate a new one... the whole process is complicated, but you can trust ListView to do a good job at it. – ivagarz Sep 28 '13 at 17:55
  • That's good. Last question, what does "recycle" mean in this context, please ? – rzqr Sep 28 '13 at 18:45
  • Oh, recycling here is just to take a View instance that, as it's no longer visible in the screen, would usually be destroyed and update it to 'build' the next row in the list. – ivagarz Sep 28 '13 at 19:29
1

Quoting from the docs

Your code might call findViewById() frequently during the scrolling ofListView, 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

Fore more info

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Also check this video on listview

http://www.youtube.com/watch?v=wDBM6wVEO70

Also listview recycles views. Check the below link for more info

How ListView's recycling mechanism works

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Why does the writer checked if convertView is null and if it's not ? – rzqr Sep 28 '13 at 17:01
  • @rzqr only if your view is null then inflate and initialize your views. just log some info in getView and see how many times the message is logged. you will know yourself how it works – Raghunandan Sep 28 '13 at 17:01
  • @rzqr watch the video in the the post you will get a clear picture – Raghunandan Sep 28 '13 at 17:04
  • @rzqr also check the last link in my post that should answer most of your questions shows a visual representation of listview recycling – Raghunandan Sep 28 '13 at 17:10
  • When I scroll down the ListView to see other elements why can take sometimes **convertView** a null value ? Doesn't it update the convertView automatically ? – rzqr Sep 28 '13 at 17:31
  • @rzqr thats because listview recycle views. what do you mean by it "doesn't update the convertView automatically". the last link shows you pictorially what happens that should answer your question – Raghunandan Sep 28 '13 at 17:33
  • I'm not comfortable with english, I can't understand each word said. I mean, what I've understood, is that convertview takes the view that has been hidden following a scroll event. So, the convertview would have as reference of the hidden view though. – rzqr Sep 28 '13 at 17:39
  • no its recycled that is what i meant by saying lsitview recycles views. views that are visible on screen are not recycled – Raghunandan Sep 28 '13 at 17:40
  • What's the mean of "recycles views", does it mean that the view will be initialized ? – rzqr Sep 28 '13 at 17:40
  • @rzqr pls go through the links in my post and i don;t know how i can explain an more in detail. – Raghunandan Sep 28 '13 at 17:54