Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we use the recyclerView.setHasFixedSize?

Tags:

android

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.

like image 571
machinezhou Avatar asked Mar 03 '15 08:03

machinezhou


People also ask

What is the use of setHasFixedSize?

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.

When should I use RecyclerView?

Use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events. If you want to use a RecyclerView , you will need to work with the following: RecyclerView. Adapter - To handle the data collection and bind it to the view.

In what situation should one use RecyclerView over ListView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.

What is the benefit of using a RecyclerView?

The major advantage of the usage of RecyclerView is the performance when loading list items in the list to the view. RecyclerView prepares the view behind and ahead beyond the visible entries. It gives significant performance when you need to fetch the bitmap image in your list from a background task.


2 Answers

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.

like image 109
Sotti Avatar answered Sep 24 '22 23:09

Sotti


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); 
like image 25
Android Team Avatar answered Sep 25 '22 23:09

Android Team