项目中,需要在Recycleview中加一行FlowLayout实现商品评价的“用户印象”模块。
RecycleView在数据改变的情况下,会刷新界面。这样可能导致刷新一次,就会执行addView一次。原来项目中的做法是先全部清除子view,再创建view并添加。但是我觉得多次全删全创建填加不太好,于是想办法复用,得到以下方法:
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){//复用
((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;