AJW
AJW

Reputation: 1649

When to use RecyclerView setHasFixedSize(true)?

I've read the Android developer info and a number of SO posts and RecyclerView articles. I'm still unclear as to whether I can use the:

recyclerView.setHasFixedSize(true);

method on my RecyclerView list. The size of my RecyclerView list will constantly change as CardView items are dynamically added and deleted over time. However, the size (height and length) of each CardViews is fixed. Please advise.

Upvotes: 5

Views: 5565

Answers (2)

JAAD
JAAD

Reputation: 12379

Yes, you can use

recyclerView.setHasFixedSize(true); 

as long as height and width of recyclerview remains the same.

From Android Developer:

public void setHasFixedSize (boolean hasFixedSize)

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. Parameters hasFixedSize true if adapter changes cannot affect the size of the RecyclerView.

Upvotes: 3

Nauman Ali Shah
Nauman Ali Shah

Reputation: 163

Its optional if you know in advance that the size of RecyclerView will remain constant once you have have set adapter for it .then use this line of code after initializing RecyclerView object otherwise don't need to use it.

Upvotes: 0

Related Questions