3

I've read some articles and watched video in order to understand how ListView works. So, let's say I have list with 5 items (all items visible on screen). I have the following code in my custom adapter which inherited from BaseAdapter:

@Override
public View getView(int position, View view, ViewGroup parent) {
    System.out.println("item position: " + position + " view " + view);

    if (view == null)
    {
        view = LayoutInflater.from(context).inflate(R.layout.list_view_item, null);
    }
    return view;
}

When I run this code I see the following output in console:

08-26 14:52:40.882: item position: 0 view null
08-26 14:52:40.883: item position: 1 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.883: item position: 2 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 0 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 1 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 2 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 0 view android.widget.RelativeLayout{88bc756 V.E...C.. ......I. 0,0-1080,65 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 1 view android.widget.RelativeLayout{1264e60 V.E...C.. ......I. 0,68-1080,133 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 2 view android.widget.RelativeLayout{36b089 V.E...C.. ......I. 0,136-1080,201 #7f0e00b8 app:id/item_container}
08-26 14:52:40.884: item position: 3 view android.widget.RelativeLayout{e1a12bb V.E...C.. ......I. 0,204-1080,269 #7f0e00b8 app:id/item_container}
08-26 14:52:40.885: item position: 4 view android.widget.RelativeLayout{ab5ffdd V.E...C.. ......I. 0,0-0,0 #7f0e00b8 app:id/item_container}

I getView() view is null only for the 0 position. After watching I supposed that I had null for the first 5 calls (until all visible item views not initialized). Why only for the first? What I am missing?

Natasha
  • 993
  • 9
  • 23

2 Answers2

1

ListView recycles views so the first run it will be null because you have to inflate the view. Once you inflate the view you should be reusing the same view and just updating the contents in order to improve performance. I would recommend watching the following video https://www.youtube.com/watch?v=wDBM6wVEO70 , they explained really well how list view works.

Amilcar Andrade
  • 1,081
  • 9
  • 16
  • This video is really useful. But I don't find the answer how `getView()` works (when it's called and how often). All that I got from it: `getView()` is called according some internal recycling algorithm but there is no any notes how it works. I understand that it may be changed but I'd like to know atleast some approximate schema. – Natasha Aug 29 '16 at 09:49
  • Well getView() will get called for each view that is visible on the screen. You can add a breakpoint and see that behavior. I would recommend going to the source code to understand exactly how the recycling algorithm works. – Amilcar Andrade Aug 29 '16 at 13:56
0

This is a factory method for the view. To prevent creating complex views over and over again you may return the created view and received it later on as parameter by the frame work.

You are supposed to check, wether you can reuse your view or not (e.g. the type matches, if you have different line visualisations) and then simply use the view for performance reasons instead of creating a new one (inflate a new one).

View inflation involves parsing XML. and is a costly operation. Thats why the API allows you to reuse components. It is a common pattern on UI development.

That is why you are having null on the first calls, and later on an instance of the view.

Melv_80
  • 222
  • 1
  • 5