36

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 android.support.v7.widget, it commented with this: 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.

What's that suppose to mean? Can anyone show me the context of using it or explain about the meaning "this category" above? thanks a lot.

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
machinezhou
  • 679
  • 2
  • 6
  • 16
  • The documentation explains it. `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.` – Pedro Oliveira Mar 03 '15 at 09:07
  • 3
    sorry, I really don't understand the follow words:"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. what kind of change, is it the size of it?if so, then what the "size" means? – machinezhou Mar 04 '15 at 04:59
  • Does your recyclerview change witdh or height according to the number of objects in your adapter? – Pedro Oliveira Mar 04 '15 at 08:42
  • 3
    most of cases , they are "match_parent". So the RView height will be the screen height, and I should use setHasFixedSize to be true, is this the "category"? – machinezhou Mar 05 '15 at 14:44
  • 1
    Yes. It's not that hard to understand that. – Pedro Oliveira Mar 05 '15 at 14:48
  • 1
    OK ,one more question please, "match_parent" will make it through because of the item reuse and preload mechanism in recyclerview, but what if the height is "wrap_content" or item count is less than the screen height? I just followed the source to the recyclerview class, and still cannot get the basic idea of optimization about this "fixedsize" method – machinezhou Mar 07 '15 at 04:56
  • finally, I got this, the optimization is the requestLayout() skipping. – machinezhou Mar 07 '15 at 05:42

2 Answers2

31

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.

Travis Yim
  • 384
  • 3
  • 14
Sotti
  • 14,089
  • 2
  • 50
  • 43
  • 7
    The term "size" is overloaded to mean 3 things: the size of the frame (ie. the scrolling view), the laid out size (ie. the scrollable area) or the numbers of items. To be crystal clear, this method refers to the first, which is usually `match_parent`. – Maarten Oct 06 '19 at 20:45
  • 1
    As @Maarten says, if we have a RecyclerView with match_parent as height, we should add setHasFixedSize(true) since the size of the RecyclerView itself does not change inserting or deleting items into it. This should be false if we have a RecyclerView with wrap_content since each element inserted by the adapter could change the size of the Recycler depending on the items inserted, so, the size of the Recycler will be different each time we add/delete items – Gastón Saillén Nov 25 '19 at 13:37
  • What if the ```layout_height``` = "0dp" to make it fill the available space? It's not going to change with adding/removing content, but it isn't the same size on each screen. – grolschie Feb 04 '20 at 23:47
  • 1
    Gastón Saillén : I have a use-case where I set the height as match-parent and if I set the setHasFixedSize(true) the contents are not shown but if the setHasFixedSize(false) then the items are shown. I tested it out with height as wrap_content and match_parent. That does not make a difference. That only decides the height of the view and not all the 3 things --> the number of items, the size of frame , the laid out area ..maybe – Dinesh Nov 13 '20 at 00:04
  • What should be this set, if we have an expandable RecyclerView, with height="match_parent", but items layout have it's height, means height="wrap_content", causes height sizes of items be different? – Reyhane Farshbaf Jun 08 '21 at 07:29
3

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);
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
  • If data is from server and the size of it is usually unexpected, in this situation we had better not set that method be true, am I right? – machinezhou Mar 04 '15 at 04:52
  • so this lead me to conclude with : if there's 20 icons and I want put them into recyclerview, this is the best "category" setting that method to be true.please correct me. – machinezhou Mar 04 '15 at 04:57
  • 3
    I consider the explanation misleading. Method setHasFixedSize(true) can be used as long as RV does not change its size due to its content. – Ευάγγελος Μπίλης Sep 07 '18 at 10:57