Understanding RecyclerView setHasFixedSize

后端 未结 8 1909
闹比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:17

    setHasFixedSize(true) means the RecyclerView has children (items) that has fixed width and height. This allows the RecyclerView to optimize better by figuring out the exact height and width of the entire list based on the your adapter.

    0 讨论(0)
  • 2020-11-28 02:18

    A very simplified version of RecyclerView has:

    void onItemsInsertedOrRemoved() {
       if (hasFixedSize) layoutChildren();
       else requestLayout();
    }
    

    This link describes why calling requestLayout might be expensive. Basically whenever items are inserted, moved or removed the size (width and height) of RecyclerView might change and in turn the size of any other view in view hierarchy might change. This is particularly troublesome if items are added or removed frequently.

    Avoid unnecessary layout passes by setting setHasFixedSize to true when changing the contents of the adapter does not change it's height or the width.


    Update: The JavaDoc has been updated to better describe what the method actually does.

    RecyclerView can perform several optimizations if it can know in advance that RecyclerView's size is not affected by the adapter contents. RecyclerView can still change its size based on other factors (e.g. its parent's size) but this size calculation cannot depend on the size of its children or contents of its adapter (except the number of items in the adapter).

    If your use of RecyclerView falls into this category, set this to {@code true}. It will allow RecyclerView to avoid invalidating the whole layout when its adapter contents change.

    @param hasFixedSize true if adapter changes cannot affect the size of the RecyclerView.

    0 讨论(0)
  • 2020-11-28 02:19

    It affects the animations of the recyclerview, if it's false.. the insert and remove animations won't show. so make sure it's true in case you added animation for the recyclerview.

    0 讨论(0)
  • 2020-11-28 02:26

    If the size of the RecyclerView (the RecyclerView itself)

    ... does not depend on the adapter content:

    mRecyclerView.setHasFixedSize(true);
    

    ...depends on the adapter content:

    mRecyclerView.setHasFixedSize(false);
    
    0 讨论(0)
  • 2020-11-28 02:29

    The ListView had a similar named function that I think did reflect info about the size of the individual list item heights. The documentation for RecyclerView pretty clearly states it is referring to the size of the RecyclerView itself, not the size of its items.

    From the RecyclerView source comment above the setHasFixedSize() method:

     * RecyclerView can perform several optimizations if it can know in advance that changes in
     * adapter content cannot change the size of the RecyclerView itself.
     * If your use of RecyclerView falls into this category, set this to true.
    
    0 讨论(0)
  • 2020-11-28 02:31

    Can confirm setHasFixedSize relates to the RecyclerView itself, and not the size of each item adapted to it.

    You can now use android:layout_height="wrap_content" on a RecyclerView, which, among other things, allows a CollapsingToolbarLayout to know it should not collapse when the RecyclerView is empty. This only works when you use setHasFixedSize(false) on the RecylcerView.

    If you use setHasFixedSize(true) on the RecyclerView, this behavior to prevent the CollapsingToolbarLayout from collapsing does not work, even though the RecyclerView is indeed empty.

    If setHasFixedSize was related to the size of items, it shouldn't have any effect when the RecyclerView has no items.

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