# RecyclerView setHasFixedSize(true); 的作用

http://www.520webtool.com/

void onItemsInsertedOrRemoved() {
if (hasFixedSize) layoutChildren();
else requestLayout();
}

RecyclerView size changes every time you add something no matter what. What setHasFixedSize does is that it makes sure (by user input) that this change of size of RecyclerView is constant. The height (or width) of the item won't change. Every item added or removed will be the same. If you dont set this it will check if the size of the item has changed and thats expensive. Just clarifying because this answer is confusing. –  ArnoldB  May 25 at 18:42

very simplified version of RecyclerView has:

void onItemsInsertedOrRemoved() {
if (hasFixedSize) layoutChildren();
else requestLayout();
}

requestLayout()是很昂贵的,因为他会要求重新布局，重新绘制（详细请看Android优化），所以如当不是瀑布流时，设置这个可以避免重复的增删造成而外的浪费资源

RecyclerView size changes every time you add something no matter what. What setHasFixedSize does is that it makes sure (by user input) that this change of size of RecyclerView is constant. The height (or width) of the item won't change. Every item added or removed will be the same. If you dont set this it will check if the size of the item has changed and thats expensive. Just clarifying because this answer is confusing. –  ArnoldB  May 25 at 18:42

RecyclerView size changes every time you add something no matter what. What setHasFixedSize does is that it makes sure (by user input) that this change of size of RecyclerView is constant. The height (or width) of the item won't change. Every item added or removed will be the same. If you dont set this it will check if the size of the item has changed and thats expensive. Just clarifying because this answer is confusing. –  ArnoldB  May 25 at 18:42

04-25 121

03-13 1万+
01-23 4万+
11-13 7958
11-09 1万+
10-31 3477
02-08 1401
04-24 8112
06-25 656