項目中,需要在Recycleview中加一行FlowLayout實現(xiàn)商品評價的“用戶印象”模塊尚揣。
RecycleView在數(shù)據(jù)改變的情況下涌矢,會刷新界面。這樣可能導(dǎo)致刷新一次快骗,就會執(zhí)行addView一次娜庇。原來項目中的做法是先全部清除子view,再創(chuàng)建view并添加滨巴。但是我覺得多次全刪全創(chuàng)建填加不太好思灌,于是想辦法復(fù)用俺叭,得到以下方法:
int impressCount =Data.size();
if (impressCount<mImpressCount){//remove
mFlowLayout.removeViews(Data.size(),mImpressCount-Data.size());
mImpressCount=impressCount;
}
for (int i = 0;i<impressCount;i++){
if (i<mImpressCount){//復(fù)用
((TextView)mFlowLayout.getChildAt(i)).setText(Data.get(i).tagName+"("+Data.get(i).count+")");
}else {//增加textview
TextView textView = (TextView) LayoutInflater.from(mFlowLayout.getContext()).inflate(R.layout.item_buyer_impression, mFlowLayout, false);
textView.setText(Data.get(i+mImpressCount).tagName+"("+Data.get(i+mImpressCount).count+")");
mFlowLayout.addView(textView);
}
}
mImpressCount =impressCount;