5

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.

Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
AJW
  • 1,578
  • 3
  • 36
  • 77
  • http://stackoverflow.com/a/40707099/1177959 – Sotti Nov 20 '16 at 17:37
  • Possible duplicate of [Understanding RecyclerView setHasFixedSize](https://stackoverflow.com/questions/28709220/understanding-recyclerview-sethasfixedsize) – Manohar Nov 01 '18 at 10:27

2 Answers2

3

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.

JAAD
  • 12,349
  • 7
  • 36
  • 57
  • I've set the RecyclerView layout_width and layout_height each to match_parent in the layout file...so not sure what that means relative to your answer. – AJW Feb 01 '16 at 06:11
  • that mean u can use it , no issue with that – JAAD Feb 01 '16 at 06:12
  • yeah I did and here's what I understood from it- It is just for internal optimization of recylerview. If you know that your list is not gonna change then you could setHasFixedSize to true. For example you are showing a dialog with static categories list. Here you are fetching the list locally and so know that the list size will not change and only operation a user can perform is select them. – krishan Feb 01 '16 at 06:15
  • he is dynamically adding and removing items not changing the size of recyclerView – JAAD Feb 01 '16 at 06:16
  • I think it is somethig to do with data set – krishan Feb 01 '16 at 06:16
  • size of recylerview mean size of dataset it is holding not its layout_height and layout_width – krishan Feb 01 '16 at 06:17
  • But the height of the RecyclerView grows over time as more and more CardViews are added so user will have to scroll down to see the CardViews. Or is the size of the RecyclerView the size of the device dimension and therefore it will not change? – AJW Feb 01 '16 at 06:29
  • if u set height to match_parent it will take full device height your adding items make the reyclerview scrollable but does not change height of recyclerView – JAAD Feb 01 '16 at 06:32
  • So can this not be used in an app that changes between portrait and landscape? – Matt Jun 05 '16 at 17:56
  • it can be used in app which changes between portrait and landscape – JAAD Jun 05 '16 at 20:05
0

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.

  • 1
    Yes but it's not clear to me what size you are referring to...the RecyclerView list I am creating will grow over time as more CardViews are added to it or is it the layout of the CardView itself which is fixed. – AJW Feb 01 '16 at 06:13
  • its about contents not layout . so you don't need to use it. – Nauman Ali Shah Feb 01 '16 at 06:19