Recycler适配器

``
public class shopAdapter extends RecyclerView.Adapter {
Context context;
ShopBean shopBean;

public shopAdapter(Context context, ShopBean shopBean) {
    this.context = context;
    this.shopBean = shopBean;
}

@NonNull
@Override
public shopAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup,  int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.shop_layout, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull shopAdapter.ViewHolder viewHolder, final int i) {
    Glide.with(context).load(shopBean.getResult().getPzsh().getCommodityList().get(i).getMasterPic())
            .into(viewHolder.img_shop);
    viewHolder.text_shop.setText(shopBean.getResult().getPzsh().getCommodityList().get(i).getCommodityName());
    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            callBackA.dianji(shopBean.getResult().getPzsh().getCommodityList().get(i).getCommodityId());
        }
    });

}

@Override
public int getItemCount() {
    return shopBean.getResult().getPzsh().getCommodityList().size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    private final ImageView img_shop;
    private final TextView text_shop;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        img_shop=itemView.findViewById(R.id.image1);
        text_shop=itemView.findViewById(R.id.text1);
    }
}
private CallBackA callBackA;

public void setCallBackA(CallBackA callBackA) {
    this.callBackA = callBackA;
}

public interface CallBackA{
    void dianji( int id);
}

}


你可能感兴趣的:(Recycler适配器)