二级列表购物车

一,依赖

compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'

二,权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

三,main  布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="ywf.bwei.com.gouwuche_lianxi.view.acativity.MainActivity">
    <include layout="@layout/layout_title" />
    <ExpandableListView
        android:id="@+id/expandList"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ExpandableListView>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginTop="2dp"
        android:background="#000000"/>
    <include layout="@layout/layout_bottom" />
</LinearLayout>


layout_title 的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_red_light"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回"
        android:textSize="25sp"
        android:padding="10dp"
        android:textColor="@android:color/white"
        android:id="@+id/btnBack"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:padding="10dp"
        android:textColor="@android:color/white"
        android:layout_weight="1"
        android:gravity="center"
        android:text="购物车"/>
    <TextView
        android:id="@+id/btnEditor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="编辑"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:padding="10dp"/>
</LinearLayout>

layout_bottom 的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全选"
        android:textSize="25sp"
        android:padding="10dp"
        android:id="@+id/btnCheckAll"/>
    <TextView
        android:id="@+id/tvTotalPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:padding="10dp"
        android:textColor="@android:color/black"
        android:layout_weight="1"
        android:gravity="center"
        android:text="合计:¥0.00"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结算(0)"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:padding="10dp"
        android:background="@android:color/holo_red_light"
        android:id="@+id/btnAmount"/>
</LinearLayout>

layout_child_item 布局


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <CheckBox
        android:id="@+id/ck_child_choose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:scaleX="0.6"
        android:scaleY="0.6" />
    <ImageView
        android:id="@+id/iv_show_pic"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:src="@mipmap/ic_launcher"
        android:layout_toRightOf="@id/ck_child_choose" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15dp"
        android:layout_toRightOf="@id/iv_show_pic"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_commodity_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="酒红色纯红色纯羊毛西服套装"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            android:textStyle="bold" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tv_commodity_attr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp"
                android:text="属性:粉蓝色"
                android:textSize="12sp"
                android:textColor="@color/colorPrimary" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tv_commodity_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="¥390"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="12sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tv_commodity_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="x1"
                android:textColor="@android:color/darker_gray" />
            <LinearLayout
                android:id="@+id/rl_edit"
                android:layout_width="120dp"
                android:background="@android:color/holo_orange_light"
                android:layout_height="30dp"
                android:layout_marginLeft="20dp"
                >
                <TextView
                    android:id="@+id/iv_sub"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:textColor="@android:color/black"
                    android:background="@android:color/white"
                    android:layout_margin="1dp"
                    android:layout_height="match_parent"
                    android:text=" - " />

                <TextView
                    android:id="@+id/tv_commodity_show_num"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:background="@android:color/white"
                    android:layout_margin="1dp"
                    android:layout_height="match_parent"
                    android:text="1"
                    />

                <TextView
                    android:id="@+id/iv_add"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_margin="1dp"
                    android:background="@android:color/white"
                    android:layout_height="match_parent"
                    android:text=" + " />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <Button
        android:id="@+id/btn_commodity_delete"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="x"
        android:background="@android:color/holo_blue_light"
        android:textSize="20sp"
        android:textColor="@android:color/holo_green_dark"
        android:layout_margin="5dp"
        android:visibility="gone" />
</RelativeLayout>

 layout_group_item 布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <CheckBox
        android:id="@+id/ck_group_choosed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="商家1"
        android:gravity="center_vertical"
        android:textSize="25sp"
        android:focusable="false"
        android:padding="10dp"/>
</LinearLayout>


四,创建  view , model , persenter , bean ,adapter包

       创建 view ,model , persenter 的接口和类

model 层的  IShoppCarModel  接口

public interface IShoppCarModel {
    //  IShooppPrensenter iShooppPrensenter是P层的接口
    void getCarInfo(String uid,IShooppPrensenter iShooppPrensenter);
}

model 的  ShoppCarModel  的类
注意:这需要OkHttp封装的utils包
public class ShoppCarModel implements IShoppCarModel {
    //IShooppPrensenter iShooppPrensenter  是P层的接口
    public void getCarInfo(String uid, final IShooppPrensenter iShooppPrensenter) {
        Map<String, String> params = new HashMap<>();
        params.put("uid", uid);
        OkHttpUtils okHttpUtils = OkHttpUtils.getInstance();
        okHttpUtils.doGet(Constants.GET_CARTS, params, new OnFinishListener() {
            @Override
            public void onFailed(String msg) {
                iShooppPrensenter.onFailed(msg);
            }
            @Override
            public void onSuccess(Object obj) {
                //Gson解析
                Gson gson = new Gson();
                ShoppCarBean shoppCarBean = gson.fromJson(obj.toString(), ShoppCarBean.class);
                iShooppPrensenter.onSuccess(shoppCarBean.getData());
            }
        });
    }
}


persenter 层的  IShooppPrensenter  接口

public interface IShooppPrensenter {
    void onSuccess(List<ShoppCarBean.DataBean> data);   //是bean里面的List集合
    void onFailed(String msg);
    void onDestory();
    void getCarInfo(String uid);
}

persenter 层的  ShooppPresenter  的类

public class ShooppPresenter implements IShooppPrensenter {
    //需要model层的接口
    private IShoppCarModel IShoppCarModel;
    private IMainView iMainView;
    //需要从P层传到V层
    public ShooppPresenter(IMainView iMainView) {
        this.iMainView = iMainView;
        //需要model层的接口
        IShoppCarModel = new ShoppCarModel();
    }
    @Override
    public void getCarInfo(String uid) {
        IShoppCarModel.getCarInfo(uid, this);
    }
    @Override
    public void onSuccess(List<ShoppCarBean.DataBean> data) {
        if (iMainView != null) {
            iMainView.onSuccess(data);
        }
    }
    @Override
    public void onFailed(String msg) {
        if (iMainView != null) {
            iMainView.onFailed(msg);
        }
    }
    @Override
    public void onDestory() {
        if (iMainView != null) {
            iMainView = null;
        }
    }
}
view 层的  IMainView  的接口

public interface IMainView {
    void onSuccess(List<ShoppCarBean.DataBean> data);
    void onFailed(String msg);
}

bean 包的 ShoppCarBean

public class ShoppCarBean {
    /**
     * msg : 请求成功
     */
    private String msg;
    private String code;
    private List<DataBean> data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * sellerName : 商家1
         * sellerid : 1
         */

        private String sellerName;
        private String sellerid;
        private List<ListBean> list;
        //商家是否被选中 组条目是否被选中
        private boolean isGroupChoosed;

        public boolean isGroupChoosed() {
            return isGroupChoosed;
        }

        public void setGroupChoosed(boolean groupChoosed) {
            isGroupChoosed = groupChoosed;
        }

        //截止到上面
        public String getSellerName() {
            return sellerName;
        }

        public void setSellerName(String sellerName) {
            this.sellerName = sellerName;
        }

        public String getSellerid() {
            return sellerid;
        }

        public void setSellerid(String sellerid) {
            this.sellerid = sellerid;
        }

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public static class ListBean {
            /**
             * 
             */

            private double bargainPrice;
            private String createtime;
            private String detailUrl;
            private String images;
            private int num;
            private int pid;
            private double price;
            private int pscid;
            private int selected;
            private int sellerid;
            private String subhead;
            private String title;
            //子条目商品是否被选中的一个字段状态
            private boolean isChildChoosed;

            public boolean isChildChoosed() {
                return isChildChoosed;
            }

            public void setChildChoosed(boolean childChoosed) {
                isChildChoosed = childChoosed;
            }

            //截止到上面
            public double getBargainPrice() {
                return bargainPrice;
            }

            public void setBargainPrice(double bargainPrice) {
                this.bargainPrice = bargainPrice;
            }

            public String getCreatetime() {
                return createtime;
            }

            public void setCreatetime(String createtime) {
                this.createtime = createtime;
            }

            public String getDetailUrl() {
                return detailUrl;
            }

            public void setDetailUrl(String detailUrl) {
                this.detailUrl = detailUrl;
            }

            public String getImages() {
                return images;
            }

            public void setImages(String images) {
                this.images = images;
            }

            public int getNum() {
                return num;
            }

            public void setNum(int num) {
                this.num = num;
            }

            public int getPid() {
                return pid;
            }

            public void setPid(int pid) {
                this.pid = pid;
            }

            public double getPrice() {
                return price;
            }

            public void setPrice(double price) {
                this.price = price;
            }

            public int getPscid() {
                return pscid;
            }

            public void setPscid(int pscid) {
                this.pscid = pscid;
            }

            public int getSelected() {
                return selected;
            }

            public void setSelected(int selected) {
                this.selected = selected;
            }

            public int getSellerid() {
                return sellerid;
            }

            public void setSellerid(int sellerid) {
                this.sellerid = sellerid;
            }

            public String getSubhead() {
                return subhead;
            }

            public void setSubhead(String subhead) {
                this.subhead = subhead;
            }

            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }
        }
    }
}

adapter 包下 MyExpandAdapter  的类

public class MyExpandAdapter extends BaseExpandableListAdapter {

    private List<ShoppCarBean.DataBean> data;
    private Context context;
    //商品加减的一个接口变量    是在下面定义的接口
    private ModifyGoodsItemNumberListener modifyGoodsItemNumberListener;
    private CheckGroupItemListener checkGroupItemListener;
    //接收是否处于编辑状态的一个boolean值
    private boolean isEditor;

    public MyExpandAdapter(Context context) {
        this.context = context;
    }


    //设置数据
    public void setList(List<ShoppCarBean.DataBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }


    //商家以及商品是否被选中的一个监听
    public void setCheckGroupItemListener(CheckGroupItemListener checkGroupItemListener) {
        this.checkGroupItemListener = checkGroupItemListener;
    }

    //设置商品的加减监听
    public void setModifyGoodsItemNumberListener(ModifyGoodsItemNumberListener modifyGoodsItemNumberListener) {
        this.modifyGoodsItemNumberListener = modifyGoodsItemNumberListener;
    }

    //是否显示删除按钮
    public void showDeleteButton(boolean isEditor) {
        this.isEditor = isEditor;
        //刷新适配器
        notifyDataSetChanged();
    }


    @Override
    public int getGroupCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data != null && data.get(groupPosition).getList() != null ? data.get(groupPosition).getList().size() : 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.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 false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_group_item, parent, false);
        }
        CheckBox ck_group_choosed = convertView.findViewById(R.id.ck_group_choosed);
        //isGroupChoosed  需要在bean定义
        if (data.get(groupPosition).isGroupChoosed()) {
            ck_group_choosed.setChecked(true);
        } else {
            ck_group_choosed.setChecked(false);
        }
        ck_group_choosed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkGroupItemListener.checkGroupItem(groupPosition, ((CheckBox) v).isChecked());
            }
        });
        //getSellerName   需要在bean定义
        ck_group_choosed.setText(data.get(groupPosition).getSellerName());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_child_item, parent, false);
        }
        //商品选择
        CheckBox ck_child_choosed = convertView.findViewById(R.id.ck_child_choose);
        //商品图片
        ImageView iv_show_pic = convertView.findViewById(R.id.iv_show_pic);
        //商品主标题
        TextView tv_commodity_name = convertView.findViewById(R.id.tv_commodity_name);
        //商品副标题
        TextView tv_commodity_attr = convertView.findViewById(R.id.tv_commodity_attr);
        //商品价格
        TextView tv_commodity_price = convertView.findViewById(R.id.tv_commodity_price);
        //商品数量
        TextView tv_commodity_num = convertView.findViewById(R.id.tv_commodity_num);
        //商品减
        TextView iv_sub = convertView.findViewById(R.id.iv_sub);
        //商品加减中的数量变化
        final TextView tv_commodity_show_num = convertView.findViewById(R.id.tv_commodity_show_num);
        //商品加
        TextView iv_add = convertView.findViewById(R.id.iv_add);
        //删除按钮
        Button btn_commodity_delete = convertView.findViewById(R.id.btn_commodity_delete);

        //设置文本信息
        tv_commodity_name.setText(data.get(groupPosition).getList().get(childPosition).getTitle());
        tv_commodity_attr.setText(data.get(groupPosition).getList().get(childPosition).getSubhead());
        tv_commodity_price.setText("¥" + data.get(groupPosition).getList().get(childPosition).getPrice());
        tv_commodity_num.setText("x" + data.get(groupPosition).getList().get(childPosition).getNum());
        tv_commodity_show_num.setText(data.get(groupPosition).getList().get(childPosition).getNum() + "");
        //分割图片地址
        String images = data.get(groupPosition).getList().get(childPosition).getImages();
        String[] urls = images.split("\\|");
        //加载商品图片
        Glide.with(context)
                .load(urls[0])
                .crossFade()
                .into(iv_show_pic);
        //商品加
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyGoodsItemNumberListener.doIncrease(groupPosition, childPosition, tv_commodity_show_num);
            }
        });
        //设置商品加减的按钮
        //商品减
        iv_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyGoodsItemNumberListener.doDecrease(groupPosition, childPosition, tv_commodity_show_num);
            }
        });
        //商品复选框是否被选中
        ck_child_choosed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkGroupItemListener.checkChildItem(groupPosition, childPosition, ((CheckBox) v).isChecked());
            }
        });
        //处理商品的选中状态
        //isChildChoosed  需要在bean定义
        if (data.get(groupPosition).getList().get(childPosition).isChildChoosed()) {
            ck_child_choosed.setChecked(true);
        } else {
            ck_child_choosed.setChecked(false);
        }

        //控制删除按钮的隐藏与显示
        /*if(isEditor){
            btn_commodity_delete.setVisibility(View.VISIBLE);
        }else{
            btn_commodity_delete.setVisibility(View.GONE);
        }*/
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    /**
     * 商家的复选框以及商品的复选框是否被选中的接口
     */
    /**
     * 商家复选框以及商品复选框是否被选中接口
     */
    public interface CheckGroupItemListener {
        //商家的条目的复选框监听
        void checkGroupItem(int groupPosition, boolean isChecked);

        //商品的
        void checkChildItem(int groupPosition, int childPosition, boolean isChecked);
    }

    /**
     * 商品加减接口
     */
    public interface ModifyGoodsItemNumberListener {
        //商品添加操作
        void doIncrease(int groupPosition, int childPosition, View view);

        //商品减少操作
        void doDecrease(int groupPosition, int childPosition, View view);

    }
}

mian 主方法


public class MainActivity extends AppCompatActivity implements IMainView, MyExpandAdapter.ModifyGoodsItemNumberListener, MyExpandAdapter.CheckGroupItemListener {

    private ExpandableListView expandList;
    private CheckBox btnCheckAll;
    private TextView tvTotalPrice;
    private TextView btnAmount;
    private TextView btnEditor;
    private MyExpandAdapter adapter;
    private IShooppPrensenter iShooppPrensenter;
    private List<ShoppCarBean.DataBean> list;
    //购买商品的总数量
    private int totalNum = 0;
    //购买商品的总价
    private double totalPrice = 0.00;
    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //隐藏actionbar
        getSupportActionBar().hide();

        expandList = (ExpandableListView) findViewById(R.id.expandList);
        //购物车底部栏的全选框
        btnCheckAll = (CheckBox) findViewById(R.id.btnCheckAll);
        //合计,计算总价
        tvTotalPrice = (TextView) findViewById(R.id.tvTotalPrice);
        //结算按钮
        btnAmount = (TextView) findViewById(R.id.btnAmount);
        //找到编辑控件
        btnEditor = (TextView) findViewById(R.id.btnEditor);
        //去除默认指示器
        expandList.setGroupIndicator(null);
        //设置适配器
        adapter = new MyExpandAdapter(this);
        expandList.setAdapter(adapter);
        //获取购物车信息
        iShooppPrensenter = new ShooppPresenter(this);
        iShooppPrensenter.getCarInfo("10192");
        //设置商品加减的监听
        adapter.setModifyGoodsItemNumberListener(this);
        //设置商家以及商品是否被选中的监听
        adapter.setCheckGroupItemListener(this);
        //全选的点击
        btnCheckAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //isChoosedAll  在下面定义的   全选与反选
                isChoosedAll(((CheckBox) view).isChecked());
                //计算商品总价
                //statisticsPrice   在下面定义的计算你所选中的商品总价
                statisticsPrice();
            }
        });
        //编辑
        /*btnEditor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(!flag){//编辑 -> 完成\
                    flag = true;
                    btnEditor.setText("完成");
                    adapter.showDeleteButton(flag);
                }else{
                    flag = false;
                    btnEditor.setText("编辑");
                    adapter.showDeleteButton(flag);
                }
            }
        });*/
    }

    //二级列表默认展开
    private void defaultExpand() {
        for (int i = 0; i < adapter.getGroupCount(); ++i) {
            expandList.expandGroup(i);
        }
    }


    @Override
    public void onSuccess(List<ShoppCarBean.DataBean> data) {
        this.list = data;
        //设置数据
        adapter.setList(list);
        defaultExpand();
    }

    @Override
    public void onFailed(String msg) {

    }

    @Override
    public void checkGroupItem(int groupPosition, boolean isChecked) {
        //Toast.makeText(this, "商家", Toast.LENGTH_SHORT).show();
        //商家javabean
        ShoppCarBean.DataBean dataBean = list.get(groupPosition);
        dataBean.setGroupChoosed(isChecked);
        //遍历商家里面的商品,将其置为选中状态
        for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) {
            listBean.setChildChoosed(isChecked);
        }
        //底部结算那个checkbox状态(全选)

        // isCheckAll  是在下面定义的  购物车商品是否全部选中
        if (isCheckAll()) {
            btnCheckAll.setChecked(true);
        } else {
            btnCheckAll.setChecked(false);
        }
        //刷新适配器
        adapter.notifyDataSetChanged();
        //计算价格
        statisticsPrice();
    }

    @Override
    public void checkChildItem(int groupPosition, int childPosition, boolean isChecked) {
        //Toast.makeText(this, "商品", Toast.LENGTH_SHORT).show();
        //ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
        ShoppCarBean.DataBean dataBean = list.get(groupPosition);//商家那一层
        List<ShoppCarBean.DataBean.ListBean> listBeans = dataBean.getList();
        ShoppCarBean.DataBean.ListBean listBean = listBeans.get(childPosition);
        //你点击商家的商品条目将其选中状态记录
        listBean.setChildChoosed(isChecked);
        //检测商家里面的每一个商品是否被选中,如果被选中,返回boolean

        //  isGoodsCheckAll  是在下面检测某个商家的商品是否都选中,如果都选中的话,商家CheckBox应该是选中状态
        boolean result = isGoodsCheckAll(groupPosition);
        if (result) {
            dataBean.setGroupChoosed(result);
        } else {
            dataBean.setGroupChoosed(result);
        }
        //底部结算那个checkbox状态(全选)
        if (isCheckAll()) {
            btnCheckAll.setChecked(true);
        } else {
            btnCheckAll.setChecked(false);
        }
        //刷新适配器
        adapter.notifyDataSetChanged();
        //计算总价
        statisticsPrice();
    }

    @Override
    public void doIncrease(int groupPosition, int childPosition, View view) {
        //Toast.makeText(this, "用户点击了商品减少操作", Toast.LENGTH_SHORT).show();
        ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
        //取出当前的商品数量
        int currentNum = listBean.getNum();
        currentNum++;
        listBean.setNum(currentNum);
        //刷新适配器
        adapter.notifyDataSetChanged();
        //计算商品价格
        statisticsPrice();
    }

    @Override
    public void doDecrease(int groupPosition, int childPosition, View view) {
        //Toast.makeText(this, "用户点击了商品减少操作", Toast.LENGTH_SHORT).show();
        ShoppCarBean.DataBean.ListBean listBean = list.get(groupPosition).getList().get(childPosition);
        //取出当前的商品数量
        int currentNum = listBean.getNum();
        //直接结束这个方法
        if (currentNum == 1) {
            return;
        }
        currentNum--;
        listBean.setNum(currentNum);
        //刷新适配器
        adapter.notifyDataSetChanged();
        //计算商品价格
        statisticsPrice();

    }

    //全选与反选
    private void isChoosedAll(boolean isChecked) {
        for (ShoppCarBean.DataBean dataBean : list) {
            dataBean.setGroupChoosed(isChecked);
            for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) {
                listBean.setChildChoosed(isChecked);
            }
        }
        //刷新适配器
        adapter.notifyDataSetChanged();
    }

    /**
     * 计算你所选中的商品总价
     */
    private void statisticsPrice() {
        //初始化值
        totalNum = 0;
        totalPrice = 0.00;
        for (ShoppCarBean.DataBean dataBean : list) {
            for (ShoppCarBean.DataBean.ListBean listBean : dataBean.getList()) {
                if (listBean.isChildChoosed()) {
                    totalNum++;
                    System.out.println("number : " + totalNum);
                    totalPrice += listBean.getNum() * listBean.getPrice();
                }
            }
        }
        //设置文本信息 合计、结算的商品数量
        tvTotalPrice.setText("合计:¥" + totalPrice);
        btnAmount.setText("结算(" + totalNum + ")");
    }

    //购物车商品是否全部选中
    private boolean isCheckAll() {

        for (ShoppCarBean.DataBean dataBean : list) {
            if (!dataBean.isGroupChoosed()) {
                return false;
            }
        }
        return true;
    }

    //检测某个商家的商品是否都选中,如果都选中的话,商家CheckBox应该是选中状态
    private boolean isGoodsCheckAll(int groupPosition) {
        List<ShoppCarBean.DataBean.ListBean> listBeans = this.list.get(groupPosition).getList();
        //遍历某一个商家的每个商品是否被选中
        for (ShoppCarBean.DataBean.ListBean listBean : listBeans) {
            if (listBean.isChildChoosed()) {//是选中状态
                continue;
            } else {
                return false;
            }
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值