仿京东购物车(二级列表)

依赖:
    compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.squareup.okhttp3:okhttp:3.10.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    compile 'io.reactivex.rxjava2:rxjava:2.1.7'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

    compile 'com.squareup.retrofit2:converter-gson:2.4.0'
    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
// 其他依赖  Fresco加载图片框架
    compile 'com.facebook.fresco:fresco:0.12.0'
    compile 'com.facebook.fresco:animated-gif:0.12.0'
    compile 'org.greenrobot:eventbus:3.0.0'
//轮播图
    compile 'com.youth.banner:banner:1.4.9'
	ApiService接口

/**
 * 购物车
 * https://www.zhaoapi.cn/product/getCarts?uid=71
 */
@GET("product/getCarts?source=android")
Flowable<ShopCartBean> getshopCart(@Query("uid") String uid);
 
 
 
 
	bean类
 
 
package com.example.com.moni.bean;

import java.util.List;

/**
 * Created by len on 2018/5/3.
 */

public class ShopCartBean {

    /**
     * msg : 请求成功
     * code : 0
     * data : [{"list":[],"sellerName":"","sellerid":"0"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":1,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":1,"pid":56,"price":99,"pscid":39,"selected":0,"sellerid":12,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家12","sellerid":"12"},{"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":20,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"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":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":41,"price":456,"pscid":2,"selected":0,"sellerid":18,"subhead":"三只松鼠零食特惠,专区满9950,满199100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/"}],"sellerName":"商家18","sellerid":"18"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":2,"pid":78,"price":999,"pscid":40,"selected":0,"sellerid":22,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP213英寸Bar i5/8G/256G"}],"sellerName":"商家22","sellerid":"22"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":2,"pid":79,"price":888,"pscid":40,"selected":0,"sellerid":23,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP213英寸Bar i5/8G/256G"}],"sellerName":"商家23","sellerid":"23"}]
     */

    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 : []
         * sellerName :
         * sellerid : 0
         */

        private String sellerName;
        private String sellerid;
        private List<ChidBean> list;
        //给一级列表checkbox设置状态值,提供set get方法
        private boolean check;

        public boolean isCheck() {
            return check;
        }

        public void setCheck(boolean check) {
            this.check = check;
        }

        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<ChidBean> getList() {
            return list;
        }

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

        public class ChidBean {

            /**
             * bargainPrice : 11800.0
             * createtime : 2017-10-10T17:33:37
             * detailUrl : https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends
             * images : https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg
             * num : 1
             * pid : 57
             * price : 5199.0
             * pscid : 40
             * selected : 0
             * sellerid : 1
             * subhead : i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统
             * title : 小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银
             */

            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;
            //child设置状态值  提供set get方法
            private boolean childCheck;

            public boolean isChildCheck() {
                return childCheck;
            }

            public void setChildCheck(boolean childCheck) {
                this.childCheck = childCheck;
            }

            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;
            }
        }
    }
}

 
 
 
 
	ShopCartModel类

 
 
public class ShopCartModel {
    //调用工具类请求数据
    public void getScartData(ShopCartPresenter shopCartPresenter,String uid) {
        ApiService apiService = HttpUtils.getInstance(BaseUrl.SHOP_CART).getApiService();
        Flowable<ShopCartBean> shopCartBeanFlowable = apiService.getshopCart(uid);
        //把数据传给p
        shopCartPresenter.success(shopCartBeanFlowable);
    }

}

 
 
	IShopCartPresenter接口

 
 
public interface IShopCartPresenter {
    //定义成功方法
    void success(Flowable<ShopCartBean> shopCartBeanFlowable);
}

	
	ShopCartPresenter类
 
 
 
 
public class ShopCartPresenter implements IShopCartPresenter{
    private  IShopCartView iShopCartView;

    //提供无参方法狗杂v层对象
    public ShopCartPresenter(IShopCartView iShopCartView){
        this.iShopCartView=iShopCartView;
    }
    //p层回调方法
    public void getCartMdata(String uid) {
        //创建m层对象
        ShopCartModel shopCartModel=new ShopCartModel();
        shopCartModel.getScartData(this,uid);
    }
    //销毁视图
    public void dettach(){
        if(iShopCartView!=null){
            iShopCartView=null;
        }
    }
    //请求方法
    @Override
    public void success(Flowable<ShopCartBean> shopCartBeanFlowable) {
        shopCartBeanFlowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableSubscriber<ShopCartBean>() {
                    @Override
                    public void onNext(ShopCartBean shopCartBean) {
                       // Log.e("--", "onNext: "+shopCartBean.getData().size());
                         iShopCartView.getSCdata(shopCartBean);
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

 
 
	IShopCartView接口
 
 
 
 
public interface IShopCartView {
    //定义请求数据方法
    void getSCdata(ShopCartBean shopCartBean);
}

 
 
 
 
	Fragment4类

 
 
package com.example.com.moni.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.com.moni.MainActivity;
import com.example.com.moni.R;
import com.example.com.moni.adapter.EXShopCartAdapter;
import com.example.com.moni.bean.ShopCartBean;
import com.example.com.moni.presenter.ShopCartPresenter;
import com.example.com.moni.view.IShopCartView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by len on 2018/4/29.
 */

public class Fragment4 extends Fragment implements IShopCartView, EXShopCartAdapter.ExShopClick {

    @BindView(R.id.ex_listview)
    ExpandableListView exListview;
    Unbinder unbinder;
    @BindView(R.id.all_box)
    CheckBox allBox;
    @BindView(R.id.all_price)
    TextView allPrice;
    @BindView(R.id.bianji)
    TextView bianji;
    @BindView(R.id.jiesuan)
    Button jiesuan;

    private ShopCartPresenter shopCartPresenter;
    private EXShopCartAdapter adapter;
    private List<ShopCartBean.DataBean> data;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment04, container, false);
        unbinder = ButterKnife.bind(this, view);
        SharedPreferences sp = getActivity().getSharedPreferences("shop", Context.MODE_PRIVATE);
        int uid = sp.getInt("uid", 0);

        if (uid == 0) {
            startActivity(new Intent(getActivity(), MainActivity.class));
        } else {
            //创建p层请求数据
            shopCartPresenter = new ShopCartPresenter(this);

            shopCartPresenter.getCartMdata(uid + "");
        }

        //设置全选

        allBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(allBox.isChecked()){
                    for (int i = 0; i <data.size() ; i++) {

                        ShopCartBean.DataBean dataBean = data.get(i);

                        //bean中的checkbox状态值设为true

                        dataBean.setCheck(true);


                        for (int j = 0; j <dataBean.getList().size() ; j++) {

                            List<ShopCartBean.DataBean.ChidBean> list = dataBean.getList();

                            //循环遍历得到childbox状态值为true

                            list.get(j).setChildCheck(true);
                        }
                    }

                }else{
                    for (int i = 0; i <data.size() ; i++) {

                        ShopCartBean.DataBean dataBean = data.get(i);

                        //bean中的checkbox状态值设为false

                        dataBean.setCheck(false);

                        for (int j = 0; j <dataBean.getList().size() ; j++) {

                            List<ShopCartBean.DataBean.ChidBean> list = dataBean.getList();

                            //循环遍历得到childbox状态值为true

                            list.get(j).setChildCheck(false);
                        }
                    }
                }
                //调用总价方法
                setPrices();
            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

//得到购物车请求数据方法
    @Override
    public void getSCdata(ShopCartBean shopCartBean) {
        data = shopCartBean.getData();
        Log.d("--", "getSCdata: " + data.size());
        //设置适配器

        getExShopAdapter();

        for (int i = 0; i < data.size(); i++) {
            exListview.expandGroup(i);
        }


    }
//设置适配器方法
  public   void getExShopAdapter() {
        //为空时擦黄建适配器
        if (adapter == null) {
            adapter = new EXShopCartAdapter(getActivity(), data);
            exListview.setAdapter(adapter);
            adapter.setShopClick(this);
        } else {
            //不为空时刷新适配器
            adapter.notifyDataSetChanged();
        }
    }

//定义数量增加方法
    @Override
    public void addNum(int group, int child) {

        List<ShopCartBean.DataBean.ChidBean> list = data.get(group).getList();

        int num = list.get(child).getNum();

        num++;

        list.get(child).setNum(num);

        //调用计算总价方法

        setPrices();

        //调用刷新适配器方法

        getExShopAdapter();

    }

//计算总价方法

    private void setPrices() {

        int price = 0;
        int num = 0;

        for (int i = 0; i < data.size(); i++) {
            //得到父类list

            List<ShopCartBean.DataBean.ChidBean> list = data.get(i).getList();
            //循环遍历得到子类list

            for (int j = 0; j < list.size(); j++) {

                //如果子list全选中时计算总价

                if (list.get(j).isChildCheck()) {
                    //计算总价方法
                    price += list.get(j).getNum() * list.get(j).getPrice();
                    num += list.get(j).getNum();
                }
            }
            //赋值总价
            allPrice.setText("¥"+price+"");
            jiesuan.setText("结算("+num+"");

        }


        getExShopAdapter();

    }

    //定义减少数量方法

    @Override
    public void deleteNum(int group, int child) {
        //得到父的list

        List<ShopCartBean.DataBean.ChidBean> list = data.get(group).getList();

        int num = list.get(child).getNum();

        num--;

        if (num == 1) {
            Toast.makeText(getActivity(), "数量已经为1无法继续减少", Toast.LENGTH_SHORT).show();
        }
        //设置数量
        list.get(child).setNum(num);

        setPrices();

    }

    //实现适配器接口子checkbox选中状态方法

    @Override
    public void childChecked(int group, int child, boolean checked) {

        List<ShopCartBean.DataBean.ChidBean> list = data.get(group).getList();

        list.get(child).setChildCheck(checked);

        boolean b = setAllCheckBox(group);

        if (b) {
            data.get(group).setCheck(true);
        } else {
            data.get(group).setCheck(false);
        }

        setPrices();
    }

    public boolean setAllCheckBox(int group) {

        List<ShopCartBean.DataBean.ChidBean> list = data.get(group).getList();

        for (int i = 0; i < list.size(); i++) {
            if (!list.get(i).isChildCheck()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void groupChecked(int group, boolean checked) {

        ShopCartBean.DataBean dataBean = data.get(group);
        dataBean.setCheck(checked);
        if (dataBean.isCheck()) {
            List<ShopCartBean.DataBean.ChidBean> list = dataBean.getList();

            for (int i = 0; i < list.size(); i++) {
                list.get(i).setChildCheck(true);
            }

        } else {
            List<ShopCartBean.DataBean.ChidBean> list = dataBean.getList();

            for (int i = 0; i < list.size(); i++) {
                list.get(i).setChildCheck(false);
            }
        }
        setPrices();


    }

//点击事件
    @OnClick({R.id.bianji, R.id.jiesuan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bianji:

                break;
            case R.id.jiesuan:

                break;
        }
    }
}

	fragment布局
 
 
<?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="match_parent"
    android:background="#fff"
    android:orientation="vertical"
   >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="购物车"
            android:textSize="20sp"
            android:textColor="#f00"
            android:background="#fff"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:id="@+id/bianji"/>
    </RelativeLayout>
    <!--二级列表-->
<ExpandableListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9.5"
    android:childIndicator="@null"
    android:groupIndicator="@null"
    android:id="@+id/ex_listview"></ExpandableListView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/all_box"
            android:text="全选"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:text="总价"
           />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:text="0"
            android:id="@+id/all_price"/>
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="结算"
            android:id="@+id/jiesuan"
            android:background="#f00"
            />
    </LinearLayout>
    </LinearLayout>
	商家复选框布局(二级列表父布局)radio_but_item.xml
 
 
 
 
<?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="match_parent"
    android:orientation="horizontal">
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ex_box"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="商家1"
        android:id="@+id/ex_shangjia"/>
</LinearLayout>

	二级列表子布局 shop_cart_item.xml
 
 
 
 
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:background="#F0F0F0"
    android:orientation="vertical">

    <LinearLayout
            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:id="@+id/child_box"
                />
            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/child_img"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="haha"
                    android:id="@+id/child_title"
                    android:layout_marginLeft="20dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="haha"
                    android:id="@+id/child_price"
                    android:textColor="#f00"
                    android:layout_marginTop="50dp"
                    android:layout_marginLeft="20dp"/>

            </LinearLayout>

        </LinearLayout>
    <LinearLayout
        android:layout_marginLeft="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentRight="true"
        android:layout_marginTop="50dp"
        >
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="-"
            android:id="@+id/child_jian"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="2"
            android:background="#D6D7D7"
            android:gravity="center"
            android:id="@+id/child_num"
            />
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="+"
            android:id="@+id/child_jia"/>
    </LinearLayout>

</RelativeLayout>
 
 
购物车适配器

 
 
package com.example.com.moni.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.com.moni.R;
import com.example.com.moni.bean.ShopCartBean;

import java.util.List;

/**
 * Created by len on 2018/5/3.
 */

public class EXShopCartAdapter extends BaseExpandableListAdapter{
    Context context;
    List<ShopCartBean.DataBean> data;
    public EXShopCartAdapter(Context context, List<ShopCartBean.DataBean> data) {
     this.context=context;
     this.data=data;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return data.get(i).getList().size();
    }

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

    @Override
    public Object getChild(int i, int i1) {
        return data.get(i).getList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final ExViewHolder exViewHolder;
        if(view==null){
            view=View.inflate(context,R.layout.radio_but_item,null);
            exViewHolder=new ExViewHolder();
            exViewHolder.z_box = view.findViewById(R.id.ex_box);
            exViewHolder.shangjia = view.findViewById(R.id.ex_shangjia);
            view.setTag(exViewHolder);
        }else{
            exViewHolder= (ExViewHolder) view.getTag();
        }
        ShopCartBean.DataBean dataBean = data.get(i);
        boolean check = dataBean.isCheck();
        exViewHolder.z_box.setChecked(check);
        exViewHolder.shangjia.setText(data.get(i).getSellerName());

        exViewHolder.z_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopClick.groupChecked(i,exViewHolder.z_box.isChecked());
            }
        });

        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final ExChildViewHolder exChildViewHolder;
        if(view==null){
            view=View.inflate(context,R.layout.shop_cart_item,null);
            exChildViewHolder=new ExChildViewHolder();

            exChildViewHolder.child_box = view.findViewById(R.id.child_box);
            exChildViewHolder.child_img = view.findViewById(R.id.child_img);
            exChildViewHolder.child_title = view.findViewById(R.id.child_title);
            exChildViewHolder.child_price = view.findViewById(R.id.child_price);
            exChildViewHolder.child_jian = view.findViewById(R.id.child_jian);
            exChildViewHolder.child_jia = view.findViewById(R.id.child_jia);
            exChildViewHolder.child_num = view.findViewById(R.id.child_num);
            view.setTag(exChildViewHolder);
        }else{
            exChildViewHolder= (ExChildViewHolder) view.getTag();

        }
        List<ShopCartBean.DataBean.ChidBean> list = data.get(i).getList();

        exChildViewHolder.child_box.setChecked(list.get(i1).isChildCheck());
        String images = list.get(i1).getImages();
        String[] split = images.split("\\|");
        Glide.with(context).load(split[0]).into(exChildViewHolder.child_img);
        exChildViewHolder.child_title.setText(list.get(i1).getTitle());
        exChildViewHolder.child_price.setText("¥"+list.get(i1).getPrice()+"");

        if(list.get(i1).getNum() == 1){
            exChildViewHolder.child_jian.setEnabled(false);
        } else{
            exChildViewHolder.child_jian.setEnabled(true);
        }

        exChildViewHolder.child_num.setText(list.get(i1).getNum()+"");

        exChildViewHolder.child_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                shopClick.addNum(i,i1);

            }
        });

        exChildViewHolder.child_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopClick.deleteNum(i,i1);
            }
        });

        exChildViewHolder.child_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                shopClick.childChecked(i,i1,exChildViewHolder.child_box.isChecked());

            }
        });


        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    //一级列表
    class ExViewHolder{
        CheckBox z_box;
        TextView shangjia;
    }
    //二级列表
    class ExChildViewHolder{
        CheckBox child_box;
        ImageView child_img;
        TextView child_title,child_price,child_num;
        Button child_jian,child_jia;

    }

    //1:
    private ExShopClick shopClick;

    public void setShopClick(ExShopClick shopClick){
        this.shopClick = shopClick;
    }

    public interface ExShopClick{

        //添加数量
        void addNum(int group,int child);
        //减少数量
        void deleteNum(int group,int child);

        //子项复选框选中状态
        void childChecked(int group,int child,boolean checked);

        //父项复选框选中状态
        void groupChecked(int group,boolean checked);
    }


}

 
 

猜你喜欢

转载自blog.csdn.net/lucky_7777777/article/details/80187395
今日推荐