Understanding RecyclerView setHasFixedSize

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.


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.


Simple Explanation

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 because each element inserted by the adapter could change the size of the RecyclerView depending on the items inserted/deleted, so, the size of the RecyclerView will be different each time we add/delete items.

To be more clear, if we use a fixed width/height

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

We can use my_recycler_view.setHasFixedSize(true)

Then if we do not use a fixed width/height

<android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

We should use my_recycler_view.setHasFixedSize(false) since wrap_content for width or height can change the size of our RecyclerView.

When we talk about RecyclerView setHasFixedSize we are not talking about the quantity of elements inside of it but instead the the size of the View itself.