在某个条目中动态添加多个小布局条目

时间:2022-08-05 19:44:23
//获取数据源
List<PromotionTagBean> promotionTag = listGoodsBean.getPromotionTag();
int size = promotionTag.size();
//holder.mAddLabel : 条目中的线性布局linearlayout
//添加之前先移除
holder.mAddLabel.removeAllViews();
if (size > 0 && promotionTag != null) {
for (int i = 0; i < size; i++) {
View view = View.inflate(context, R.layout.category_acticity, null);
//设置参数
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
param.setMargins(Utils.dip2px(context, 5), 0, 0, Utils.dip2px(context, 10));
view.setLayoutParams(param);
ImageView iv = (ImageView) view.findViewById(R.id.iv_prom_logo);
TextView tv = (TextView) view.findViewById(R.id.tv_prom_desc);
tv.setSingleLine();
tv.setEllipsize(TextUtils.TruncateAt.END);
setPic(iv, promotionTag.get(i).getPromotionLogo());
tv.setText(promotionTag.get(i).getPromotionDesc());
//循环添加各个条目子布局
holder.mAddLabel.addView(view);
}
}