RecyclerView setHasFixedSize(true); 的作用

来源:互联网 发布:游光网络拉勾网 编辑:程序博客网 时间:2024/06/06 18:26

下文翻译:RecyclerView的尺寸在每次改变时,比如你加任何些东西。setHasFixedSize 的作用就是确保尺寸是通过用户输入从而确保RecyclerView的尺寸是一个常数。RecyclerView 的Item宽或者高不会变。每一个Item添加或者删除都不会变。如果你没有设置setHasFixedSized没有设置的代价将会是非常昂贵的。因为RecyclerView会需要而外计算每个item的size,


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
2 4
原创粉丝点击