实现价格联动

public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartAdapter.ChildHolder> implements View.OnClickListener {
private Context context;
private List goodsList;

public ShopCartAdapter(Context context) {
    this.context = context;
    this.goodsList = new ArrayList<>();
}

@NonNull
@Override
public ChildHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.goods_item, null);
    return new ChildHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ChildHolder childHolder, int i) {
    Log.i("aaa","的点点滴滴");
    Goods goods = goodsList.get(i);
    childHolder.goods_name.setText(goods.commodityName);
    Glide.with(context).load(goods.pic).into(childHolder.goods_img);
    childHolder.g_add.setOnClickListener(this);
    childHolder.g_add.setTag(goods);
    childHolder.g_reduce.setOnClickListener(this);
    childHolder.g_reduce.setTag(goods);
    childHolder.goods_check.setOnClickListener(this);
    childHolder.goods_check.setChecked(goods.check);
    childHolder.goods_check.setTag(goods);

    childHolder.num.setText(goods.count+"");
}

@Override
public int getItemCount() {
    return goodsList.size();
}

public void addAll(List<Goods> data) {
    if (data!=null){
        goodsList.addAll(data);
    }
}

@Override
public void onClick(View v) {
    Goods s = (Goods) v.getTag();
    if (v.getId()==R.id.goods_check){
        s.check = ((CheckBox) v).isChecked();
    }else if (v.getId()==R.id.g_add){
        s.count++;
    }else if (v.getId()==R.id.g_reduce){
        if (s.count>1){
            s.count--;
        }
    }
    //计算价格
    calculatePrice();
    notifyDataSetChanged();
}

public void calculatePrice() {
    double sum = 0;
    for (int i = 0; i < goodsList.size(); i++) {
        Goods goods = goodsList.get(i);
        if (goods.check) {
            sum = sum+goods.count*goods.price;
        }
    }
    if (priceListener != null) {
        priceListener.priceListener(sum);
    }
}

public List<Goods> getList() {
    return goodsList;
}

private PriceListener priceListener;

public void setPriceListener(PriceListener priceListener) {
    this.priceListener = priceListener;
}

public interface PriceListener{
    void priceListener(double sum);
}

public class ChildHolder extends RecyclerView.ViewHolder {

    private final CheckBox goods_check;
    private final ImageView goods_img;
    private final TextView goods_name;
    private final Button g_add;
    private final Button g_reduce;
    private final TextView num;

    public ChildHolder(@NonNull View itemView) {
        super(itemView);
        goods_check = itemView.findViewById(R.id.goods_check);
        goods_img = itemView.findViewById(R.id.goods_img);
        goods_name = itemView.findViewById(R.id.goods_name);
        g_add = itemView.findViewById(R.id.g_add);
        g_reduce = itemView.findViewById(R.id.g_reduce);
        num = itemView.findViewById(R.id.num);
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值