购物车

private Context context;
private ShoppCardBean shoppCardBean;
private CheckBox checkAll;
private Button jiesuan;
private TextView zongjia;
private List<ShoppCardBean.DataBean> data;
public MyExAdapter(Context context, ShoppCardBean shoppCardBean, CheckBox checkAll, TextView zongjia,Button jiesuan) {
    this.context = context;
    this.shoppCardBean = shoppCardBean;
    this.checkAll = checkAll;
    this.zongjia = zongjia;
    this.jiesuan = jiesuan;
}
@Override
public int getGroupCount() {
    return shoppCardBean.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
    return shoppCardBean.getData().get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
    return shoppCardBean.getData().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
    return shoppCardBean.getData().get(groupPosition).getList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}
@Override
public boolean hasStableIds() {
    return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    GroupHolder groupHolder = null;
    if (convertView == null) {
        groupHolder = new GroupHolder();
        convertView= LayoutInflater.from(context).inflate(R.layout.group_layout, null);
        groupHolder.groupText = convertView.findViewById(R.id.group_sellerName);
        //绑定
        convertView.setTag(groupHolder);
    }else{
        groupHolder = (GroupHolder) convertView.getTag();
    }
    //赋值
    groupHolder.groupText.setText(shoppCardBean.getData().get(groupPosition).getSellerName());
    return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ChildHolder childHolder = null;
    if (convertView == null) {
        childHolder = new ChildHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.ex_child_layout, null);
        childHolder.child_name = convertView.findViewById(R.id.child_name);
        childHolder.ex_child_simple = convertView.findViewById(R.id.ex_child_simple);
        childHolder.child_price = convertView.findViewById(R.id.child_price);
        childHolder.jian = convertView.findViewById(R.id.jian);
        childHolder.add = convertView.findViewById(R.id.add);
        childHolder.child_check = convertView.findViewById(R.id.child_check);
        childHolder.count = convertView.findViewById(R.id.count);
        //绑定
        convertView.setTag(childHolder);
    }else{
        childHolder = (ChildHolder) convertView.getTag();
    }
    //赋值
    data = shoppCardBean.getData();
    childHolder.child_name.setText(data.get(groupPosition).getList().get(childPosition).getTitle());
    childHolder.child_price.setText("¥"+ data.get(groupPosition).getList().get(childPosition).getPrice());
    childHolder.count.setText(data.get(groupPosition).getList().get(childPosition).getNum()+"");
    childHolder.ex_child_simple.setImageURI(data.get(groupPosition).getList().get(childPosition).getImages().split("\\|")[0]);
    //点击加号数量进行++
    final ChildHolder finalChildHolder = childHolder;
    AddCount(groupPosition, childPosition, childHolder, finalChildHolder);
    //点击减号数量进行--,到1的时候不在进行--
    final ChildHolder finalChildHolder1 = childHolder;
    JianCount(groupPosition, childPosition, childHolder, finalChildHolder1);
    //获取多选框的状态
    if (data.get(groupPosition).getList().get(childPosition).getSelected()==1) {
        childHolder.child_check.setChecked(true);
    } else{
        childHolder.child_check.setChecked(false);
    }
    //点击结算进行下单
    jiesuan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            jiesuanCallBack.jiesuanclick(data.get(groupPosition).getList().get(childPosition).getPrice());
        }
    });
    //点击多选框进行计算总价
    checkboxSums(groupPosition, childPosition, childHolder);
    //点击子条目进行弹框
    clichitemAlert(groupPosition, childPosition, convertView);

    return convertView;
}

private void clichitemAlert(final int groupPosition, final int childPosition, View convertView) {
    convertView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(context);
            View view = View.inflate(context, R.layout.alertdialog_layout, null);
            builder.setView(view);
            builder.setCancelable(true);
            Button quxiao = view.findViewById(R.id.quxiao);
            Button queding = view.findViewById(R.id.queding);
            final AlertDialog dialog = builder.create();
            dialog.show();
            quxiao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            queding.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    childItemLongClickCallBack.itemclick(data.get(groupPosition).getList().get(childPosition).getPid());
                    dialog.dismiss();

                }
            });
            return false;
        }
    });
}


public void Allcheck() {

        double sums = 0;
        for (int i = 0; i < data.size(); i++) {
            List<ShoppCardBean.DataBean.ListBean> list = data.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                ShoppCardBean.DataBean.ListBean listBean = list.get(j);
                    sums += list.get(j).getNum() * list.get(j).getPrice();
            }
        }
        zongjia.setText("总价:¥"+sums);
    priceAllCallBack.itemAllPrice(sums);

}

private void checkboxSums(final int groupPosition, final int childPosition, final ChildHolder childHolder) {
    childHolder.child_check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double sums = 0;
            int flag = 0;
            int itemcount=0;
            //首先获取到多选框的状态
            int selected = data.get(groupPosition).getList().get(childPosition).getSelected();
            if (selected == 0) {
                data.get(groupPosition).getList().get(childPosition).setSelected(1);
            }else{
                data.get(groupPosition).getList().get(childPosition).setSelected(0);
            }
            for (int i = 0; i < data.size(); i++) {//循环父类
                List<ShoppCardBean.DataBean.ListBean> list = data.get(i).getList();
                //循环子类
                for (int j = 0; j < list.size(); j++) {

                    ShoppCardBean.DataBean.ListBean listBean = list.get(j);
                    if (listBean.getSelected() == 1) {
                        sums += listBean.getNum() * listBean.getPrice();
                        flag++;
                    }
                    itemcount++;
                }

            }
            if (flag ==itemcount) {
                checkAll.setChecked(true);
            }else{
                checkAll.setChecked(false);

            }
            //赋值
            zongjia.setText("总价:¥"+sums);
        }
    });
}
//点击减号数量进行--,到1的时候不在进行--

private void JianCount(final int groupPosition, final int childPosition, final ChildHolder childHolder, final ChildHolder finalChildHolder1) {
    childHolder.jian.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            double sums = 0;

            if (data.get(groupPosition).getList().get(childPosition).getNum() == 1) {
                childHolder.child_check.setChecked(false);

                Toast.makeText(context,"亲,最后一件商品了,不能再减了",Toast.LENGTH_LONG).show();
            }else{
                data.get(groupPosition).getList().get(childPosition).setNum(data.get(groupPosition).getList().get(childPosition).getNum() - 1);

                finalChildHolder1.count.setText(data.get(groupPosition).getList().get(childPosition).getNum()+"");
            }
            //点击减价格随之改变
            for (int i = 0; i < data.size(); i++) {//循环父类
                List<ShoppCardBean.DataBean.ListBean> list = data.get(i).getList();

                for (int j = 0; j < list.size(); j++) {
                    ShoppCardBean.DataBean.ListBean listBean = list.get(j);
                    if (listBean.getSelected() == 1) {
                        sums += listBean.getNum() * listBean.getPrice();
                    }
                }
            }
            zongjia.setText("总价:¥"+sums);
        }
    });
}
//点击加号数量进行++
private void AddCount(final int groupPosition, final int childPosition, final ChildHolder childHolder, final ChildHolder finalChildHolder) {
    childHolder.add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double sums = 0;

            //给bean类里面的数量set值
            data.get(groupPosition).getList().get(childPosition).setNum(data.get(groupPosition).getList().get(childPosition).getNum()+1);
           Allcheck();
           notifyDataSetChanged();
            if (data.get(groupPosition).getList().get(childPosition).getNum() > 1) {
                childHolder.child_check.setChecked(true);
            }
            //给count控件赋值
            finalChildHolder.count.setText(data.get(groupPosition).getList().get(childPosition).getNum()+"");
            //点击减价格随之改变
            for (int i = 0; i < data.size(); i++) {//循环父类
                List<ShoppCardBean.DataBean.ListBean> list = data.get(i).getList();
                //循环子类
                for (int j = 0; j < list.size(); j++) {
                    ShoppCardBean.DataBean.ListBean listBean = list.get(j);
                    if (listBean.getSelected() == 1) {
                        sums += listBean.getNum() * listBean.getPrice();
                    }

                }
            }
            zongjia.setText("总价:¥"+sums);
        }
    });
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
class GroupHolder{
    TextView groupText;
}
class ChildHolder{
    CheckBox child_check;
    SimpleDraweeView ex_child_simple;
    TextView child_name,child_price,count,jian,add;
}
//定义接口
public  interface  ChildItemLongClickCallBack{
    void itemclick(int pid);
}
//定义价格接口
public interface  JiesuanCallBack{
    void jiesuanclick(double price);
}

private JiesuanCallBack jiesuanCallBack;

public void setJiesuanCallBack(JiesuanCallBack jiesuanCallBack) {
    this.jiesuanCallBack = jiesuanCallBack;
}

//定义计算总价接口
public  interface  PriceAll{
    void itemAllPrice(double nums);
}
private PriceAll priceAllCallBack;

public void setPriceAllCallBack(PriceAll priceAllCallBack) {
    this.priceAllCallBack = priceAllCallBack;
}

private ChildItemLongClickCallBack childItemLongClickCallBack;

public void setChildItemLongClickCallBack(ChildItemLongClickCallBack childItemLongClickCallBack) {
    this.childItemLongClickCallBack = childItemLongClickCallBack;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值