Understanding RecyclerView setHasFixedSize

后端 未结 8 2015
闹比i
闹比i 2020-11-28 01:52

I\'m having some trouble understanding setHasFixedSize(). I know that it is used for optimization when the size of RecyclerView doesn\'t change, fr

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 02:34

    If we have a RecyclerView with match_parent as height/width, we should add setHasFixedSize(true) since the size of the RecyclerView itself does not change inserting or deleting items into it.

    setHasFixedSize should be false if we have a RecyclerView with wrap_content as height/width since each element inserted by the adapter could change the size of the Recycler depending on the items inserted/deleted, so, the size of the Recycler will be different each time we add/delete items.

    To be more clear, if we use

    
    

    We can use my_recycler_view.setHasFixedSize(true)

    
    

    We should use my_recycler_view.setHasFixedSize(false) , this applies if we also use wrap_content as width

提交回复
热议问题