0

I have a ListView and a Adapter. Simple getView code could be found below.

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        Log.d("mzule", "convertView is null " + position);
        convertView = mInflater.inflate(R.layout.item_cheap_layout, null);
    }
    return convertView;
}

When I build and run the app. I got logs below.

D/mzule   (25020): converView is null a 0
D/mzule   (25020): converView is null a 1
D/mzule   (25020): converView is null a 2
D/mzule   (25020): converView is null a 3
D/mzule   (25020): converView is null a 4
D/mzule   (25020): converView is null a 5
D/mzule   (25020): converView is null a 16
D/mzule   (25020): converView is null a 32
D/mzule   (25020): converView is null a 47
D/mzule   (25020): converView is null a 38

It is obvious that previous 6 converViews are null. But I could not figure out why 16th, 32nd, 47th and 38th convertView is null. It make my ListView laggy.

Cao Dongping
  • 969
  • 1
  • 12
  • 29
  • Seems like no one get the question ... my assumption is that you understand the recycling ... **Any chances that the height of item may be different?** – Selvin Sep 21 '15 at 09:41
  • @Selvin Yes, different height for some items. It will impact on convertView? Should I use different viewType for different height item? – Cao Dongping Sep 21 '15 at 10:52
  • *It will impact on convertView?* IMHO, yes ... just follow such example ... for the items from 0 to 14 height is 2 ... so there is 5 visible items and 1 more needed to transition (fx there a 4 full visible items and 2 partial - first and last) ... now row 15 height is 1.5 .... listview need one more ... at 31 there are 2(or 3) rows that takes 1.5 ... etc .... *Should I use different viewType for different height item?* if view is not completely different then no... *It make my ListView laggy* ... <= about this part ... I don;t think if "convertView is null" would be the problem ... – Selvin Sep 21 '15 at 11:18
  • @Selvin If item height is the problem. I think it will only lag several times, and while `scrapViews` is full enough, lags will gone. I wonder that if my item view is complicated, inflate time may be 30ms. Is there some chance that my item view is too complicated to be cached enough? – Cao Dongping Sep 21 '15 at 12:31

3 Answers3

-1

Take a look at here. It is very good explaination how ListView's recycling mechanism works.

Community
  • 1
  • 1
mr.icetea
  • 2,607
  • 3
  • 24
  • 42
-1

At first, the caller of the method getView will put the params like this:

getView(0,null,parent);

the caller(AdapterView) will not put which is returned from getView the view to convertView until you has return a view in getView. At first, it will put null to convertView.

xxxzhi
  • 471
  • 3
  • 12
-1

You need to understand how listview works. Layout inflations are expensive. Listview works on the concept of view recycling. Listview uses scrap-views for non-visible views. Every time ListView needs to show a new row on screen, it will call the getView() method from its adapter. Once you scroll and a row goes-off the screen which is moved to recyclable pool while one row is added to screen. If the incoming row is in recyclable pool, it will get the view from pool otherwise new row is created. Hope it helps :)

  • *You need to understand how listview works.* You **need to learn reading with understanding** ... in the question we have *It is obvious that previous 6 converViews are null. But I could not figure out why 16th, 32nd, 47th* ... <= it implicated that **he does understand recycling** ... what he doesn't understand is why on 32,47, etc convertView is still null and this "answer" doesn't explain it – Selvin Sep 21 '15 at 09:42
  • There is nothing wrong with his code. Only thing is he can use ViewHolder pattern to remove lag. What do you think ? – Manish kumar Sep 21 '15 at 09:52
  • Where did you explain this: *But I could not figure out why 16th, 32nd, 47th and 38th convertView is null.* in your answer? please read the question again: **there is no null values from 6 to 15 .... so the asker think: starting from 6 all view should be recycled ... and he is getting null convertview at 16 ... He is asking why?** – Selvin Sep 21 '15 at 09:55