When do we use the recyclerView.setHasFixedSize?

前端 未结 2 1912
旧巷少年郎
旧巷少年郎 2020-12-14 15:41

here\'s the thing: Anybody know the setHasFixedSize method? some says that it allows for optimizations if all items are of the same size, and in RecyclerView class from andr

相关标签:
2条回答
  • 2020-12-14 15:52

    setHasFixedSize() is used to let the RecyclerView keep the same size.

    This information will be used to optimize itself.

    Here is reference url

    http://antonioleiva.com/recyclerview/

    Example:

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
    recyclerView.setHasFixedSize(true);
    
    0 讨论(0)
  • 2020-12-14 16:11

    It's interesting for the RecyclerView to know if its size (width and height dimensions) depends on the adapter content to avoid expensive layout operations. If the RecyclerView knows in advance that its size doesn't depend on the adapter content, then it will skip checking if its size should change every time an item is added or removed from the adapter. This is especially important because inserting an deleting elements can happen very often.

    If the size of the RecyclerView (the RecyclerView itself)...

    ...doesn't depend on the adapter content:

    mRecyclerView.setHasFixedSize(true);
    

    ...depends on the adapter content:

    mRecyclerView.setHasFixedSize(false);
    

    If you check the RecyclerView class you'll see it in more details because as of right now mHasFixedSize isn't used in that many places in that class.

    Setting it as true doesn't mean that the RecyclerView size is fixed, just means it won't change because of change in the adapter content. For example the RecyclerView size can change because of a size change on its parent.

    0 讨论(0)
提交回复
热议问题