码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView条目的点击事件

时间:2017-12-10 20:22:38      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:事件   定义   you   url   false   blog   get   osi   listener   

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<GoodsBean.GoodsListBean> list;
private Context context;
//2、定义一个属性
private OnItemClickListener onItemClickListener;

//1、接口回调第一步,先定义一个接口
public interface OnItemClickListener {
public void onItemClick(int a);
}

//3、定义一个方法
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}

public MyAdapter(List<GoodsBean.GoodsListBean> list, Context context) {
this.list = list;
this.context = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
MyViewHolder my = (MyViewHolder) holder;
Uri uri = Uri.parse(list.get(position).getImage_url()+"");
my.img.setImageURI(uri);
my.tv_01.setText(list.get(position).getGoods_name());
my.tv_02.setText("¥"+list.get(position).getGroup().getPrice()+"");
my.ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(list.get(position).getGoods_id());
}
});
}


@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{

private final SimpleDraweeView img;
private final TextView tv_01;
private final TextView tv_02;
private LinearLayout ll;

public MyViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
tv_01 = itemView.findViewById(R.id.tv_01);
tv_02 = itemView.findViewById(R.id.tv_02);
ll = itemView.findViewById(R.id.ll);
}
}
}

RecyclerView条目的点击事件

标签:事件   定义   you   url   false   blog   get   osi   listener   

原文地址:http://www.cnblogs.com/ysxy/p/8017815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!