给循环往布局中添加的控件添加点击事件

项目中需求,根据服务器返回的数量在布局上显示相应的控件个数,通过服务器返回的数量循环添加控件个数,并且需要对这些数据设置点击事件

很简单,在这些自定义控件或者是控件中添加一个tag,并对其绑定点击事件,在onClick方法中取出Tag,并做出相应的反应

代码如下

myHomeSaleLayout.setTag(hotGoods);
myHomeSaleLayout.setOnClickListener(this);

onClick:
Object tag = view.getTag();
if(tag!=null){
BaseHotGoods.HotGoods hotGoods = (BaseHotGoods.HotGoods)tag;
Gson gson = new Gson();
String json = gson.toJson(hotGoods);
Intent intent = new Intent(mActivity, BaseDetailActivity.class);
intent.putExtra("Json",json);
intent.putExtra("tag","goods");
mActivity.startActivity(intent);
}
跳转到相对应的页面
 
原文地址:https://www.cnblogs.com/kim-liu/p/7525086.html