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);
}
}
}