购物车

<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="com.bwie.lian.gouwuche.ShopCartActivity">

    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/white"
        android:gravity="center_vertical">

        <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:focusable="false" />

        <TextView
            android:id="@+id/tv_qx"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/checkbox2"
            android:gravity="center_vertical"
            android:text="全选"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@id/tv_qx"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="总价:0.0"
                android:textColor="#000"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/tv_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="共0件商品"
                android:textColor="#000"
                android:textSize="15sp" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_jiesuan"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/shape"
            android:gravity="center"
            android:padding="10dp"
            android:text="去结算"
            android:textColor="@android:color/white"
            android:textSize="15sp" />
    </RelativeLayout>

</LinearLayout>
 

 
bean

public class AddCartBean {
    private String msg;
    private String code;

    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 class ShopCartBean {
    /**
     * msg : 请求成功
     * code : 0
     * data : [{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":20,"price":599,"pscid":1,"selected":1,"sellerid":13,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家13","sellerid":"13"}]
     */

    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 {
        /**
         * list : [{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":20,"price":599,"pscid":1,"selected":1,"sellerid":13,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}]
         * sellerName : 商家13
         * sellerid : 13
         */
        private boolean checked;
        private String sellerName;
        private String sellerid;
        private List<ListBean> list;

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        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 {
            /**
             * bargainPrice : 111.99
             * createtime : 2017-10-14T21:39:05
             * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends
             * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg
             * num : 1
             * pid : 20
             * price : 599
             * pscid : 1
             * selected : 1
             * sellerid : 13
             * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下
             * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g
             */
            private boolean checked;
            private double bargainPrice;
            private String createtime;
            private String detailUrl;
            private String images;
            private int num;
            private int pid;
            private String price;
            private int pscid;
            private int selected;
            private int sellerid;
            private String subhead;
            private String title;

            public boolean isChecked() {
                return checked;
            }

            public void setChecked(boolean checked) {
                this.checked = checked;
            }

            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 String getPrice() {
                return price;
            }

            public void setPrice(String 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;
            }
        }
    }
}
MessageEvent
public class MessageEvent {
    private boolean checked;

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
PriceAndCountEvent
public class PriceAndCountEvent {
    private float price;
    private int count;

    public float getPrice() {
        return price;
    }

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

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
IShopCartModel

public interface IShopCartModel {
    public void getGoods(String uid, String token,OnNetListener<ShopCartBean> onNetListener);
}
ShopCartModel

public class ShopCartModel implements IShopCartModel{

    @Override
    public void getGoods(String uid, String token, final OnNetListener<ShopCartBean> onNetListener) {
        ServiceApi serviceApi = RetrofitHelper.getServiceApi();
        serviceApi.getGoods(uid,token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ShopCartBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        onNetListener.onFailure((Exception) e);
                    }

                    @Override
                    public void onNext(ShopCartBean shopCartBean) {
                        onNetListener.onSuccess(shopCartBean);
                    }
                });
    }
}
IShopCartView

public interface IShopCartView {
    public void showList(List<ShopCartBean.DataBean> groupList, List<List<ShopCartBean.DataBean.ListBean>> childList);
}
ShopCartPresenter

public class ShopCartPresenter {
    private final IShopCartModel iShopCartModel;
    private IShopCartView iShopCartView;

    public ShopCartPresenter(IShopCartView iShopCartView) {
        this.iShopCartView = iShopCartView;
        iShopCartModel = new ShopCartModel();
    }

    public void getGoods(String uid, String token) {
        iShopCartModel.getGoods(uid, token, new OnNetListener<ShopCartBean>() {
            @Override
            public void onSuccess(ShopCartBean shopCartBean) {
                List<ShopCartBean.DataBean> groupList = shopCartBean.getData();
                List<List<ShopCartBean.DataBean.ListBean>> childList = new ArrayList<>();
                for (int i = 0; i < groupList.size(); i++) {
                    List<ShopCartBean.DataBean.ListBean> list = groupList.get(i).getList();
                    childList.add(list);
                }
                iShopCartView.showList(groupList, childList);
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    public void detachView() {
        if (iShopCartView != null) {
            iShopCartView = null;
        }
    }
}
适配器布局

1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/cb_parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="30dp"
        android:focusable="false" />

    <TextView
        android:id="@+id/tv_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:background="@drawable/title" />

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="京东自营" />


</LinearLayout>
2.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/cb_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:focusable="false" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/iv_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:padding="10dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_tel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="三只松鼠_零食大礼包"
                android:textColor="#000" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="颜色:黑色" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/tv_pri"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:text="¥3000.00"
                        android:textColor="#ff00" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="80dp"
                        android:orientation="horizontal"
                        android:padding="10dp">

                        <ImageView
                            android:id="@+id/iv_del"
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:src="@drawable/shopcart_minus_grey" />

                        <TextView
                            android:id="@+id/tv_numm"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:background="@drawable/shopcart_add_btn"
                            android:paddingBottom="2dp"
                            android:paddingLeft="20dp"
                            android:paddingRight="20dp"
                            android:paddingTop="2dp"
                            android:text="1" />

                        <ImageView
                            android:id="@+id/iv_add"
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:layout_marginLeft="5dp"
                            android:src="@drawable/shopcart_add_red" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:background="#000" />

    <TextView
        android:id="@+id/tv_del"
        android:layout_width="35dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@drawable/rublish" />

</LinearLayout>

适配器

public class ShopCartAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<ShopCartBean.DataBean> groupList;
    private List<List<ShopCartBean.DataBean.ListBean>> childList;
    private LayoutInflater inflater;

    public ShopCartAdapter(Context context, List<ShopCartBean.DataBean> groupList, List<List<ShopCartBean.DataBean.ListBean>> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).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) {
        //声明GroupViewHolder
        View view;
        final GroupViewHolder holder;
        //判断convertView是否为空
        if (convertView == null) {
            //初始化GroupViewHolder
            holder = new GroupViewHolder();
            //使用布局加载器加载写好的布局
            view = inflater.inflate(R.layout.group_item, null);
            //加载布局中的组件
            holder.cbGroup = (CheckBox) view.findViewById(R.id.cb_parent);
            holder.tv_number = (TextView) view.findViewById(R.id.tv_number);
            //关联GroupViewHolder
            view.setTag(holder);
        } else {
            //如果convertView不为空,就取出参数
            view = convertView;
            holder = (GroupViewHolder) view.getTag();
        }
        //给获取到的组件赋值
        final ShopCartBean.DataBean dbGroup = groupList.get(groupPosition);
        holder.cbGroup.setChecked(dbGroup.isChecked());
        holder.tv_number.setText(dbGroup.getSellerName());

        //设置一级列表checkbox的点击事件
        holder.cbGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置checkbox的选中状态
                dbGroup.setChecked(holder.cbGroup.isChecked());
                //当全选选中时,所有的二级列表的checkbox被选中
                changeChildCbState(groupPosition, holder.cbGroup.isChecked());
                //当全选选中时,发送数量和价格
                EventBus.getDefault().post(compute());
                //一级列表的checkbox全选
                changeAllCbState(isAllGroupCbSelected());
                //刷新列表
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        //声明ChildViewHolder
        View view;
        final ChildViewHolder holder;
        //判断convertView是否为空
        if (convertView == null) {
            //初始化GroupViewHolder
            holder = new ChildViewHolder();
            //使用布局加载器加载写好的布局
            view = inflater.inflate(R.layout.child_item, null);
            //加载布局中的组件
            holder.cbChild = (CheckBox) view.findViewById(R.id.cb_child);
            holder.tv_tel = (TextView) view.findViewById(R.id.tv_tel);
            holder.iv_image = (SimpleDraweeView) view.findViewById(R.id.iv_image);
            holder.tv_pri = (TextView) view.findViewById(R.id.tv_pri);
            holder.tv_content = (TextView) view.findViewById(R.id.tv_content);
            holder.iv_del = (ImageView) view.findViewById(R.id.iv_del);
            holder.iv_add = (ImageView) view.findViewById(R.id.iv_add);
            holder.tv_number = (TextView) view.findViewById(R.id.tv_numm);
            holder.tv_del = (TextView) view.findViewById(R.id.tv_del);
            //关联GroupViewHolder
            view.setTag(holder);
        } else {
            //如果convertView不为空,就取出参数
            view = convertView;
            holder = (ChildViewHolder) view.getTag();
        }
        //给获取到的组件赋值
        final ShopCartBean.DataBean.ListBean lbChild = childList.get(groupPosition).get(childPosition);
        holder.cbChild.setChecked(lbChild.isChecked());
        holder.tv_tel.setText(lbChild.getTitle());
        holder.tv_content.setText(lbChild.getSubhead().substring(0,15)+"...");
        holder.tv_pri.setText("¥" + lbChild.getPrice());
        holder.tv_number.setText(lbChild.getNum() + "");
        String images = lbChild.getImages();
        String[] strings = images.split("\\|");
        holder.iv_image.setImageURI(strings[0]);

        //设置二级列表checkbox的点击事件
        holder.cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置该条目对象里的checkbox的属性
                lbChild.setChecked(holder.cbChild.isChecked());
                //调用方法
                PriceAndCountEvent priceAndCountEvent = compute();
                //发送消息
                EventBus.getDefault().post(priceAndCountEvent);
                //判断二级列表checkbox的状态值
                if (holder.cbChild.isChecked()) {
                    //当前checkbox是选中状态
                    changeGroupCbState(groupPosition, true);
                    changeAllCbState(isAllGroupCbSelected());
                } else {
                    //当前checkbox是未选中状态
                    changeGroupCbState(groupPosition, false);
                    changeAllCbState(isAllGroupCbSelected());
                }
                //刷新列表
                notifyDataSetChanged();
            }
        });

        //加号
        holder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到数量num
                int num = lbChild.getNum();
                //动态改变num的值
                holder.tv_number.setText(++num + "");
                lbChild.setNum(num);
                //判断二级列表的checkbox是否选中
                if (holder.cbChild.isChecked()) {
                    //选中的时候讲价格和数量返回
                    PriceAndCountEvent priceAndCountEvent = compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });

        //减号
        holder.iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //得到数量num
                int num = lbChild.getNum();
                if (num == 1) {
                    return;
                }
                //动态改变num的值
                holder.tv_number.setText(--num + "");
                lbChild.setNum(num);
                //判断二级列表的checkbox是否选中
                if (holder.cbChild.isChecked()) {
                    //选中的时候讲价格和数量返回
                    PriceAndCountEvent priceAndCountEvent = compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });

        //删除
        holder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ShopCartBean.DataBean.ListBean> listBeen = childList.get(groupPosition);
                ShopCartBean.DataBean.ListBean remove = listBeen.remove(childPosition);
                if (listBeen.size() == 0) {
                    childList.remove(groupPosition);
                    groupList.remove(groupPosition);
                }
                EventBus.getDefault().post(compute());
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    //一级列表的ViewHolder
    class GroupViewHolder {
        CheckBox cbGroup;
        TextView tv_number;
    }

    //二级列表的ViewHolder
    class ChildViewHolder {
        CheckBox cbChild;
        TextView tv_tel;
        SimpleDraweeView iv_image;
        TextView tv_pri;
        TextView tv_content;
        TextView tv_time;
        ImageView iv_del;
        ImageView iv_add;
        TextView tv_number;
        TextView tv_del;
    }

    /**
     * 改变全选的状态
     *
     * @param flag
     */
    private void changeAllCbState(boolean flag) {
        //创建MessageEvent
        MessageEvent messageEvent = new MessageEvent();
        //赋值
        messageEvent.setChecked(flag);
        //发送全选的状态值
        EventBus.getDefault().post(messageEvent);
    }

    /**
     * 改变一级列表checkbox的状态
     *
     * @param groupPosition
     * @param flag
     */
    private void changeGroupCbState(int groupPosition, boolean flag) {
        ShopCartBean.DataBean dataBean = groupList.get(groupPosition);
        dataBean.setChecked(flag);
    }

    /**
     * 改变二级列表checkbox的状态
     *
     * @param groupPosition
     * @param flag
     */
    public void changeChildCbState(int groupPosition, boolean flag) {
        List<ShopCartBean.DataBean.ListBean> listBeans = childList.get(groupPosition);
        for (int i = 0; i < listBeans.size(); i++) {
            ShopCartBean.DataBean.ListBean listBean = listBeans.get(i);
            listBean.setChecked(flag);
        }
    }

    /**
     * 判断一级列表checkbox是否全部选中
     *
     * @return
     */
    public boolean isAllGroupCbSelected() {
        for (int i = 0; i < groupList.size(); i++) {
            ShopCartBean.DataBean dataBean = groupList.get(i);
            if (!dataBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断二级列表checkbox是否全部选中
     *
     * @param groupPosition
     * @return
     */
    private boolean isAllChildCbSelected(int groupPosition) {
        List<ShopCartBean.DataBean.ListBean> childLb = childList.get(groupPosition);
        for (int i = 0; i < childLb.size(); i++) {
            ShopCartBean.DataBean.ListBean listBean = childLb.get(i);
            if (!listBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 计算列表中,选中的钱和数量
     */
    private PriceAndCountEvent compute() {
        //设置变量
        int count = 0;
        float price = 0;
        //遍历
        for (int i = 0; i < childList.size(); i++) {
            List<ShopCartBean.DataBean.ListBean> lb = childList.get(i);
            for (int j = 0; j < lb.size(); j++) {
                ShopCartBean.DataBean.ListBean listBean = lb.get(j);
                if (listBean.isChecked()) {
                    //给变量设置数据
                    Float aFloat = Float.valueOf(listBean.getPrice());
                    price += aFloat * listBean.getNum();
                    count += listBean.getNum();
                }
            }

        }

        //创建PriceAndCountEvent
        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
        //给PriceAndCountEvent添加数据
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        //返回PriceAndCountEvent
        return priceAndCountEvent;
    }

    /**
     * 设置全选、反选
     *
     * @param flag
     */
    public void changeAllListCbState(boolean flag) {
        //遍历所有的checkbox
        for (int i = 0; i < groupList.size(); i++) {
            //调用方法
            changeGroupCbState(i, flag);
            changeChildCbState(i, flag);
        }
        //发送价格和数量
        EventBus.getDefault().post(compute());
        //刷新列表
        notifyDataSetChanged();
    }
}
Activity

public class ShopCartActivity extends AppCompatActivity implements IShopCartView{

    private CheckBox mCheckbox2;
    private TextView mTvQx;
    private TextView mTvPrice;
    private TextView mTvNum;
    private ShopCartAdapter shopCartAdapter;
    private ShopCartPresenter shopCartPresenter;
    private ExpandableListView mElv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_cart);

        initView();

        EventBus.getDefault().register(this);

        shopCartPresenter = new ShopCartPresenter(this);
        int uid = MyApp.preferences.getInt("uid", 0);
        String token = MyApp.preferences.getString("token", "");
        shopCartPresenter.getGoods(uid + "", token);

        //设置全选的checkbox的点击事件
        mCheckbox2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shopCartAdapter.changeAllListCbState(mCheckbox2.isChecked());
            }
        });
    }

    private void initView() {
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);
        mTvQx = (TextView) findViewById(R.id.tv_qx);
        mTvPrice = (TextView) findViewById(R.id.tv_price);
        mTvNum = (TextView) findViewById(R.id.tv_num);
    }

    @Override
    public void showList(List<ShopCartBean.DataBean> groupList, List<List<ShopCartBean.DataBean.ListBean>> childList) {
        //创建适配器
        shopCartAdapter = new ShopCartAdapter(this, groupList, childList);
        //关联,展示数据
        mElv.setAdapter(shopCartAdapter);
        //去掉条目前面的箭头
        mElv.setGroupIndicator(null);
        //默认让其全部展开
        for (int i = 0; i < groupList.size(); i++) {
            mElv.expandGroup(i);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

        if (shopCartPresenter != null) {
            shopCartPresenter.detachView();
        }
    }

    @Subscribe
    public void onMessageEvent(MessageEvent event) {
        mCheckbox2.setChecked(event.isChecked());
    }

    @Subscribe
    public void onMessageEvent(PriceAndCountEvent event) {
        mTvPrice.setText("总价:¥" + event.getPrice());
        mTvNum.setText("共" + event.getCount() + "件商品");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值