Understanding RecyclerView setHasFixedSize

AndroidAndroid 5.0-LollipopAndroid Recyclerview

Android Problem Overview


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

What does that mean though? In most common cases a ListView almost always has a fixed size. In what cases would it not be a fixed size? Does it mean that the actual real estate that it occupies on screen grows with the content?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

When we set setHasFixedSize(true) on RecyclerView that means recycler's size is fixed and is not affected by the adapter contents. And in this case onLayout is not called on recycler when we update the adaptrer's data (but there is an exception).

Let's go to the example:

RecyclerView has a RecyclerViewDataObserver (find default implemntation in this file) with several methods, the main important is:

void triggerUpdateProcessor() {
    if (POST_UPDATES_ON_ANIMATION && mHasFixedSize && mIsAttached) {
        ViewCompat.postOnAnimation(RecyclerView.this, mUpdateChildViewsRunnable);
    } else {
        mAdapterUpdateDuringMeasure = true;
        requestLayout();
    }
}

This method is called if we set setHasFixedSize(true) and update an adapter's data via: notifyItemRangeChanged, notifyItemRangeInserted, notifyItemRangeRemoved or notifyItemRangeMoved. In this case there is no calls to the recycler's onLayout, but there is calls to requestLayout for updating childs.

But if we set setHasFixedSize(true) and update an adapter's data via notifyItemChanged then there is call to onChange of the recycler's default RecyclerViewDataObserver and no calls to triggerUpdateProcessor. In this case the recycler onLayout is called whenever we set setHasFixedSize true or false.

// no calls to triggerUpdateProcessor
@Override
public void onChanged() {
    assertNotInLayoutOrScroll(null);
     mState.mStructureChanged = true;

     processDataSetCompletelyChanged(true);
     if (!mAdapterHelper.hasPendingUpdates()) {
         requestLayout();
     }
}

// calls to triggerUpdateProcessor
@Override
public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
    assertNotInLayoutOrScroll(null);
    if (mAdapterHelper.onItemRangeChanged(positionStart, itemCount, payload)) {
        triggerUpdateProcessor();
    }
}

How to check by yourself:

Create custom RecyclerView and override:

override fun requestLayout() {
    Log.d("CustomRecycler", "requestLayout is called")
    super.requestLayout()
}

override fun invalidate() {
    Log.d("CustomRecycler", "invalidate is called")
    super.invalidate()
}

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
    Log.d("CustomRecycler", "onLayout is called")
    super.onLayout(changed, l, t, r, b)
}

Set the recycler size to match_parent (in xml). Try to update adapter's data using replaceData and replaceOne with seting setHasFixedSize(true) and then false.

// onLayout is called every time
fun replaceAll(data: List<String>) {
    dataSet.clear()
    dataSet.addAll(data)
    this.notifyDataSetChanged()
}

// onLayout is called only for setHasFixedSize(false)
fun replaceOne(data: List<String>) {
    dataSet.removeAt(0)
    dataSet.addAll(0, data[0])
    this.notifyItemChanged(0)
}

And check your log.

My log:

// for replaceAll
D/CustomRecycler: requestLayout is called
D/CustomRecycler: onMeasure is called
D/CustomRecycler: onMeasure is called
D/CustomRecycler: onLayout
D/CustomRecycler: requestLayout is called
D/CustomRecycler: requestLayout is called
D/CustomRecycler: onDraw is called

// for replaceOne
D/CustomRecycler: requestLayout is called
D/CustomRecycler: onDraw is called
D/CustomRecycler: requestLayout is called
D/CustomRecycler: onDraw is called

Summarize:

If we set setHasFixedSize(true) and update adapter's data with notifying an observer in some other way than calling notifyDataSetChanged, then you have some perfomance, because the is no calls to the recycler onLayout method.

Solution 6 - Android

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.

Solution 7 - Android

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);

Solution 8 - Android

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.

Solution 9 - Android

RecyclerView size changes every time you add something no matter what.

What setHasFixedSize does is that it makes sure (by user input) that this change of size of RecyclerView is constant. The height (or width) of the item won't change. Every item added or removed will be the same.

If you don't set this it will check if the size of the item has changed and thats expensive. (From a comment)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSIr CodealotView Question on Stackoverflow
Solution 1 - AndroidLukaCikoView Answer on Stackoverflow
Solution 2 - AndroidGastón SaillénView Answer on Stackoverflow
Solution 3 - AndroidKevinView Answer on Stackoverflow
Solution 4 - AndroiddangVarmitView Answer on Stackoverflow
Solution 5 - AndroidbitvaleView Answer on Stackoverflow
Solution 6 - AndroidAlaa AbuZarifaView Answer on Stackoverflow
Solution 7 - AndroidAlok SinghView Answer on Stackoverflow
Solution 8 - AndroidCalvin ParkView Answer on Stackoverflow
Solution 9 - AndroidLukas NiessenView Answer on Stackoverflow