When do we use recyclerView.setHasFixedSize?

here's the thing: Does anyone know the setHasFixedSize method? some say that it allows you to optimize if all elements are the same size, and in the RecyclerView class from android.support.v7.widget, he commented on this: RecyclerView can perform several optimizations if it can know in advance that the changes to the adapter contents cannot resize RecyclerView itself. If your use of RecyclerView falls into this category, set this to true.

What does this mean? Can someone show me the context of use or explain the meaning of “this category” above? Thank you very much.

+14
android
Mar 03 '15 at 8:55
source share
2 answers

Interestingly, RecyclerView knows if its size (width and height dimensions) depends on the contents of the adapter to avoid costly layout operations. If the RecyclerView knows in advance that its size does not depend on the contents of the adapter, it will skip checking if its size should be changed every time an element is added or removed from the adapter. This is especially important since insertion of deletion elements can occur very often.

If the size of the RecyclerView (of the RecyclerView itself) ...

... does not depend on the contents of the adapter:

mRecyclerView.setHasFixedSize(true); 

... depends on the contents of the adapter:

 mRecyclerView.setHasFixedSize(false); 

If you check the RecyclerView class , you will see it in more detail, because at the moment mHasFixedSize is not used in many places in this class.

Setting it as true does not mean that the size of the RecyclerView is fixed, it simply means that it will not change due to changes in the contents of the adapter. For example, the size of a RecyclerView may change due to the resizing of its parent.

+8
Nov 20 '16 at 5:31 on
source share

setHasFixedSize () is used to keep RecyclerView the same size .

This information will be used for optimization .

Here is url link

http://antonioleiva.com/recyclerview/

Example:

 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list); recyclerView.setHasFixedSize(true); 
+5
Mar 03 '15 at 9:55
source share



All Articles