Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

android - When do we use the recyclerView.setHasFixedSize?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...