下文翻譯:RecyclerView的尺寸在每次改變時,比如你加任何些東西烫扼。setHasFixedSize 的作用就是確保尺寸是通過用戶輸入從而確保RecyclerView的尺寸是一個常數(shù)。RecyclerView 的Item寬或者高不會變性锭。每一個Item添加或者刪除都不會變启涯。如果你沒有設(shè)置setHasFixedSized沒有設(shè)置的代價將會是非常昂貴的。因為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.–ArnoldBMay 25 at 18:42
verysimplified version of RecyclerView has:
void onItemsInsertedOrRemoved() {
if (hasFixedSize) layoutChildren();
else requestLayout();
}
requestLayout()是很昂貴的,因為他會要求重新布局,重新繪制(詳細請看Android優(yōu)化)锌畸,所以如當(dāng)不是瀑布流時勇劣,設(shè)置這個可以避免重復(fù)的增刪造成而外的浪費資源
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.–ArnoldBMay 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.–ArnoldBMay 25 at 18:42