why does the ViewHolder pattern work?

前端 未结 3 759
逝去的感伤
逝去的感伤 2020-12-10 09:50

I learned Android\'s ArrayAdapter today, and find there is a commom pattern which uses a ViewHolder to hold Views\' reference instead of calling findViewById everytime.

3条回答
  •  一个人的身影
    2020-12-10 10:13

    I believe the work beneath the list view is something like this (considering we have only one item view type):

    do once:

    inflate item view from layout, cache it
    

    repeat for every item:

    ask adapter to fill the data into the view
    draw the view on the screen
    move to next item
    

    so you have the view which is inflated from xml layout and can be reused for drawing multiple list items. ViewHolder speeds it up a bit more by saving getViewById lookups.

提交回复
热议问题