总结大全


//IView

public interface IView {
    void onSuccess(Object o);
    void onFailed(Exception e);

    void ondeleSuccess(Object o);
    void ondeleFailed(Exception e);
}


//Carbean

public class Carbean {
    /**
     * msg : 请求成功
     * code : 0
     * data : [{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","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":1,"pid":60,"price":13888,"pscid":40,"selected":0,"sellerid":4,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"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":48,"price":222,"pscid":39,"selected":0,"sellerid":4,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家4","sellerid":"4"}]
     */
    private String msg;
    private String code;
    private List<DataBean> data;

    public Carbean(String msg, String code, List<DataBean> data) {
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    @Override
    public String toString() {
        return "Carbean{" +
                "msg='" + msg + '\'' +
                ", code='" + code + '\'' +
                ", data=" + 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 {
        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }
        /**
         * list : [{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","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":1,"pid":60,"price":13888,"pscid":40,"selected":0,"sellerid":4,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"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":48,"price":222,"pscid":39,"selected":0,"sellerid":4,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}]
         * sellerName : 商家4
         * sellerid : 4
         */
        private boolean checked = true;
        private String sellerName;
        private String sellerid;
        private List<ListBean> list;

        public DataBean(String sellerName, String sellerid, List<ListBean> list) {
            this.sellerName = sellerName;
            this.sellerid = sellerid;
            this.list = list;
        }

        @Override
        public String toString() {
            return "DataBean{" +
                    "sellerName='" + sellerName + '\'' +
                    ", sellerid='" + sellerid + '\'' +
                    ", list=" + list +
                    '}';
        }

        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 {
            public boolean isChecked() {
                return checked;
            }

            public void setChecked(boolean checked) {
                this.checked = checked;
            }
            /**
             * bargainPrice : 11800.0
             * createtime : 2017-10-14T21:48:08
             * 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 : 1
             * pid : 60
             * price : 13888.0
             * pscid : 40
             * selected : 0
             * sellerid : 4
             * subhead : 购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)
             * title : 全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G
             */
            private boolean checked = true;
            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;

            @Override
            public String toString() {
                return "ListBean{" +
                        "bargainPrice=" + bargainPrice +
                        ", createtime='" + createtime + '\'' +
                        ", detailUrl='" + detailUrl + '\'' +
                        ", images='" + images + '\'' +
                        ", num=" + num +
                        ", pid=" + pid +
                        ", price=" + price +
                        ", pscid=" + pscid +
                        ", selected=" + selected +
                        ", sellerid=" + sellerid +
                        ", subhead='" + subhead + '\'' +
                        ", title='" + title + '\'' +
                        '}';
            }

            public ListBean(double bargainPrice, String createtime, String detailUrl, String images, int num, int pid, double price, int pscid, int selected, int sellerid, String subhead, String title) {
                this.bargainPrice = bargainPrice;
                this.createtime = createtime;
                this.detailUrl = detailUrl;
                this.images = images;
                this.num = num;
                this.pid = pid;
                this.price = price;
                this.pscid = pscid;
                this.selected = selected;
                this.sellerid = sellerid;
                this.subhead = subhead;
                this.title = title;
            }

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

// Carfubean

public class Carfubean {
    private String shopName;
    private boolean isFatherflag;

    public Carfubean(String shopName, boolean isFatherflag) {
        this.shopName = shopName;
        this.isFatherflag = isFatherflag;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public boolean isFatherflag() {
        return isFatherflag;
    }

    public void setFatherflag(boolean fatherflag) {
        isFatherflag = fatherflag;
    }
}

// Carsonbean

public class Carsonbean {
    private String createtime;
    private String images;
    private int num;
    private int pid;
    private double price;
    private String subhead;
    private String title;
    private boolean isChildFlag;

    public Carsonbean(String createtime, String images, int num, int pid, double price, String subhead, String title, boolean isChildFlag) {
        this.createtime = createtime;
        this.images = images;
        this.num = num;
        this.pid = pid;
        this.price = price;
        this.subhead = subhead;
        this.title = title;
        this.isChildFlag = isChildFlag;
    }

    public String getCreatetime() {
        return createtime;
    }

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

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

    public boolean isChildFlag() {
        return isChildFlag;
    }

    public void setChildFlag(boolean childFlag) {
        isChildFlag = childFlag;
    }
}

// Deletecar

public class Deletecar {
    /**
     * msg : 加购成功
     * code : 0
     */

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

// MessageEvent

public class MessageEvent {
    private boolean checkd=true;
    public boolean isCheckd(){
        return checkd;
    }
    public void  setCheckd(boolean checkd){
        this.checkd=checkd;
    }
}

// PriceAndCountEvent

public class PriceAndCountEvent {
    private int price;
    private int count;
    private int to;

    public int getPrice() {
        return price;
    }

    public int getCount() {
        return count;
    }

    public int getTo(){
        return  to;
    }

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

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

    public void setTo(int to) {
        this.to = to;
    }
}

// ApiService

public interface ApiService {
    //http://120.27.23.105/product/getProductCatagory?cid=1
    //        s = "http://120.27.23.105/product/getCarts?source=android&uid=3515";
    @GET("product/getCarts")
    Flowable<Carbean> get(@QueryMap Map<String,String> map);

    @GET("product/deleteCart")
    Flowable<Deletecar> dele(@QueryMap Map<String,String> map);

}


// BasePresenter

public interface BasePresenter {
    void get(String baseurl,Map<String,String> map);
    void deletecar(String baseurl,Map<String,String> map);
}

// Presenter

public class Presenter implements BasePresenter{

    private IView iv;
    private DisposableSubscriber subscriber;

    public Presenter(IView iv) {
        this.iv = iv;
    }

    @Override
    public void get(String baseurl,Map<String, String> map) {
        Model model = new Model(this);
        model.get(baseurl,map);
    }

    @Override
    public void deletecar(String baseurl, Map<String, String> map) {
        Model model = new Model(this);
        model.deletecar(baseurl,map);
    }

    public void getDele(Flowable flowable){
        subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        iv.ondeleSuccess(o);
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.ondeleFailed((Exception) t);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getData(Flowable flowable){
        subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        iv.onSuccess(o);
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFailed((Exception) t);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //防止内存泄漏
    public void detatch(){
        if (iv != null) {
            iv = null;
        }
        if(subscriber!=null){
            if(!subscriber.isDisposed()){
                subscriber.dispose();
            }
        }
    }

}

// IModel

public interface IModel {
    void get(String baseurl,Map<String,String> map);
    void deletecar(String baseurl,Map<String,String> map);

}
//Model
public class Model implements IModel{

    private Presenter presenter;

    public Model(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void get(String baseurl, Map<String, String> map) {
        Flowable<Carbean> flowable = RetrofitUtils
                .getInstance(baseurl)
                .getretrofit()
                .create(ApiService.class)
                .get(map);
        presenter.getData(flowable);
    }

    @Override
    public void deletecar(String baseurl, Map<String, String> map) {
        Flowable<Deletecar> flowable = RetrofitUtils
                .getInstance(baseurl)
                .getretrofit()
                .create(ApiService.class)
                .dele(map);
        presenter.getDele(flowable);
    }
}

//RetrofitUtils
public class RetrofitUtils {
    private static volatile RetrofitUtils instance;
    private final Retrofit retrofit;

    private RetrofitUtils(String baseurl) {
        //创建OkHttp网络请求对象
        OkHttpClient build1 = new OkHttpClient.Builder()
                .addInterceptor(new Logger())
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(baseurl)
                .client(build1)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static RetrofitUtils getInstance(String baseurl) {
        if (instance == null) {
            synchronized (RetrofitUtils.class) {
                if (instance == null) {
                    instance = new RetrofitUtils(baseurl);
                }
            }
        }
        return instance;
    }

    public Retrofit getretrofit(){
        return retrofit;
    }

}


//Shoppingcar
public class Shoppingcar extends AppCompatActivity implements IView {
    private ExpandableListView carlistview;
    private ArrayList<Carbean.DataBean> fu_list = new ArrayList<>();
    private ArrayList<List<Carbean.DataBean.ListBean>> zi_list = new ArrayList<>();
    private TextView totalprice;
    private CheckBox chooseall;
    private ArrayList<Carsonbean> carsonbeen = new ArrayList<>();;
    private TextView price;
    private String s;
    private String jsprice;
    private String jsnum;
    private Carbean ss;
    private TextView jiesuan;
    private TextView btneditor;
    private List<Carbean.DataBean> data;
    private com.example.carlianxi.adpter.shopingcaradapter shopingcaradapter;
    private Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoppingcar);
        carlistview = (ExpandableListView) findViewById(R.id.carListView);
        totalprice = (TextView) findViewById(R.id.totalPrice);
        chooseall = (CheckBox) findViewById(R.id.chooseAll);
        price = (TextView) findViewById(R.id.totalPrice);
        jiesuan = (TextView) findViewById(R.id.btnAmount);
        presenter = new Presenter(this);
        EventBus.getDefault().register(this);

        //点击结算
//        jiesuan.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(Shoppingcar.this, JiesuanActivity.class);
//                intent.putExtra("jiaqian",jsprice);
//                intent.putExtra("shuliang",jsnum);
                Toast.makeText(Shoppingcar.this,jsprice,Toast.LENGTH_SHORT).show();
                startActivity(intent);
//                startActivityForResult(intent,1);
//            }
//        });

        btneditor = (TextView) findViewById(R.id.btnEditor);
        //点击编辑
        btneditor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(btneditor.getText().toString().trim().equals("编辑")){
                    if(!(shopingcaradapter==null)){
                        shopingcaradapter.flog=true;
                        shopingcaradapter.notifyDataSetChanged();
                    }
                    btneditor.setText("完成");
                }else {
                    if(!(shopingcaradapter==null)) {
                        shopingcaradapter.flog=false;
                        shopingcaradapter.notifyDataSetChanged();
                    }
                    btneditor.setText("编辑");
                }
            }
        });
        //全选/全不选
        chooseall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置全选
                shopingcaradapter.changeAllListCbState(chooseall.isChecked());
            }
        });
//        s = "http://120.27.23.105/product/getCarts?source=android&uid=3515";
//        carpersonlei.edcanshu(s);

        //        s = "http://120.27.23.105/product/getCarts?source=android&uid=3515";
        s = "http://120.27.23.105/";
        HashMap<String, String> map = new HashMap<>();
        map.put("uid","3515");
        presenter.get(s,map);


    }
    @Subscribe
    public void onMessageEvent(MessageEvent event) {
        chooseall.setChecked(event.isCheckd());
    }

    @Subscribe
    public void onMessageEven(PriceAndCountEvent event) {
        int price = event.getPrice();
        jsprice = price+"";
        this.price.setText("总价" +price );
        jsnum = event.getTo()+"";
        //  num.setText("共" + event.getCount() + "件商品");
        jiesuan.setText("结算: ("+event.getTo()+")");
    }

    @Override
    public void onSuccess(Object o) {
        Carbean bean = (Carbean) o;
        ss = bean;
        //获取数据源
        if(bean==null){
//            show.setVisibility(View.VISIBLE);
            Toast.makeText(Shoppingcar.this, "购物车为空,快去买东西吧",Toast.LENGTH_SHORT).show();
        }else{
            data = bean.getData();
            fu_list.addAll(data);
            for (int i = 0; i< data.size(); i++){
                List<Carbean.DataBean.ListBean> list = data.get(i).getList();
                zi_list.add(list);
            }
            shopingcaradapter = new shopingcaradapter(Shoppingcar.this, fu_list, zi_list);
            //shuzg-hi适配器
            carlistview.setAdapter(shopingcaradapter);
            //让二级列表进行展开,
            int groupCount = carlistview.getCount();
            for (int i = 0; i < groupCount; i++) {
                carlistview.expandGroup(i);
            }
        }
    }

    @Override
    public void onFailed(Exception e) {

    }

    @Override
    public void ondeleSuccess(Object o) {
        Log.e("chen", "ondeleSuccess: "+"我是删除" );
    }

    @Override
    public void ondeleFailed(Exception e) {

    }
}


//Logger
public class Logger implements Interceptor {
    //https://www.zhaoapi.cn/product/getProductDetail?source=android&pid=53
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        HttpUrl url=original.url().newBuilder()
                .addQueryParameter("source","android")
                .build();
        //添加请求头
        Request request = original.newBuilder()
                .url(url)
                .build();
        return chain.proceed(request);
    }
}

//shopingcaradapter
public class shopingcaradapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Carbean.DataBean> data1;
    private ArrayList<List<Carbean.DataBean.ListBean>> data2;
    private Boolean flag = false;
    public static boolean flog;
    public shopingcaradapter(Context context, ArrayList<Carbean.DataBean> data1, ArrayList<List<Carbean.DataBean.ListBean>> data2) {
        this.context = context;
        this.data1 = data1;
        this.data2 = data2;
        Log.e("chen", "shopingcaradapter:我是适配器的值 "+data1+data2 );
    }

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

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

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

    @Override
    public Object getChild(int i, int i1) {
        return data2.get(i).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) {
        View inflate = View.inflate(context, R.layout.twoleveedyi,null);
        TextView furongqi = inflate.findViewById(R.id.furongqi);
        furongqi.setText(data1.get(i).getSellerName());
        final CheckBox fuche = inflate.findViewById(R.id.fucheckbox);

        fuche.setChecked(data1.get(i).isChecked());
        fuche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断一级列表复选框的状态  设置为true或false
                data1.get(i).setChecked(fuche.isChecked());
                //改变二级checkbod的状态
                changeChildCbState(i,fuche.isChecked());
                //算钱
                EventBus.getDefault().post(computer());
                //改变全选状态   isAllGroupCbSelect判断一级是否全部选中
                changeAllCbState(isAllGroupCbSelect());
                //必刷新
                notifyDataSetChanged();
            }
        });

        return inflate;
    }
    @Override
    public View getChildView(final int i, final int i1, final boolean b, View view, ViewGroup viewGroup) {
        final Carbean.DataBean.ListBean listBean = data2.get(i).get(i1);
        final View inflate = View.inflate(context, R.layout.shopp_car_item, null);
        final View viewById = inflate.findViewById(R.id.btn_commodity_delete);
        final CheckBox ck_chose = inflate.findViewById(R.id.ck_chose);
        ImageView btn_commodity_delete = inflate.findViewById(R.id.btn_commodity_delete);
        if(flog){
            btn_commodity_delete.setVisibility(View.VISIBLE);
        }else{
            btn_commodity_delete.setVisibility(View.GONE);
        }

        btn_commodity_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Presenter presenter = new Presenter(new IView() {
                    @Override
                    public void onSuccess(Object o) {

                    }

                    @Override
                    public void onFailed(Exception e) {

                    }

                    @Override
                    public void ondeleSuccess(Object o) {
                        List<Carbean.DataBean.ListBean> listBeen = data2.get(i);
                        Carbean.DataBean.ListBean remove = listBeen.remove(i1);
                        if (listBeen.size() == 0) {
                            //先移除二级列表的集合,再移除一级列表的集合
                            data2.remove(i);
                            data1.remove(i);
                            notifyDataSetChanged();
                        }

//                        Toast.makeText(context,"我是得到的值"+listBean,Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void ondeleFailed(Exception e) {

                    }
                });
                Map<String, String> map = new HashMap<>();
                map.put("pid", "53");
                presenter.deletecar("http://120.27.23.105/", map);
            }
        });
//        btn_commodity_delete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
                int eduid = App.cxsp.getInt("eduid", 3515);
//                Cardeletepersonlei cardeletepersonlei = new Cardeletepersonlei(new Deletecarinit() {
//                    @Override
//                    public void seecss(String bean) {
//                        List<Carbean.DataBean.ListBean> listBeen = data2.get(i);
//                        Carbean.DataBean.ListBean remove = listBeen.remove(i1);
//                        if (listBeen.size() == 0) {
//                            //先移除二级列表的集合,再移除一级列表的集合
//                            data2.remove(i);
//                            data1.remove(i);
//                            notifyDataSetChanged();
//                        }
//
//                        Toast.makeText(context,bean,Toast.LENGTH_SHORT).show();
//                    }
//                    @Override
//                    public void faild(String shib) {
//                        Toast.makeText(context,shib,Toast.LENGTH_SHORT).show();
//                    }
//                });
//                cardeletepersonlei.edcanshu("http://120.27.23.105/product/deleteCart",3515+"",data2.get(i).get(i1).getPid()+"");
//            }
//        });

        //中间的数量
        final TextView tv_commodity_show_num = (TextView) inflate.findViewById(R.id.tv_commodity_show_num);
        //减去数量
        TextView iv_sub = inflate.findViewById(R.id.iv_sub);
        //加上数量
        TextView iv_add = inflate.findViewById(R.id.iv_add);
        //图片
        ImageView iv_show_pic = inflate.findViewById(R.id.iv_show_pic);
        //名称
        TextView tv_commodity_name = inflate.findViewById(R.id.tv_commodity_name);
        //价格
        TextView tv_commodity_price = inflate.findViewById(R.id.tv_commodity_price);
        //介绍
        TextView tv_commodity_attr = inflate.findViewById(R.id.tv_commodity_attr);

        String title = data2.get(i).get(i1).getTitle();
        double price = data2.get(i).get(i1).getPrice();
        String images = data2.get(i).get(i1).getImages();
        String subhead = data2.get(i).get(i1).getSubhead();
        int num = data2.get(i).get(i1).getNum();
        String[] split = images.split("\\|");
        Glide.with(context).load(split[1]).into(iv_show_pic);
        tv_commodity_name.setText(title);
        tv_commodity_price.setText(price+"");
        tv_commodity_attr.setText(subhead);
        tv_commodity_show_num.setText(num+"");

        //设置二级列表checkbox的属性
        ck_chose.setChecked(data2.get(i).get(i1).isChecked());
        //二级列表的点击事件
        ck_chose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置该条目中的checkbox属性值
                listBean.setChecked(ck_chose.isChecked());
                //计算价钱
                PriceAndCountEvent priceAndCountEvent = computer();
                EventBus.getDefault().post(priceAndCountEvent);
                //判断当前checkbox是选中的状态
                if (ck_chose.isChecked()) {
                    //如果全部选中(isAllChildCbSelected)
                    if (isAllChildCbSelected(i)) {
                        //改变一级列表的状态
                        changeGroupCbState(i, true);
                        //改变全选的状态
                        changeAllCbState(isAllGroupCbSelect());
                    }
                } else {
                    //如果没有全部选中,一级列表的checkbox为false不为选中
                    changeGroupCbState(i, false);
                    changeAllCbState(isAllGroupCbSelect());
                }
                notifyDataSetChanged();
            }
        });

        //加号
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = listBean.getNum();
                //num为int类型所以要加空字符串
                tv_commodity_show_num.setText(++num+"");
                listBean.setNum(num);
                //如果二级列表的checkbox为选中,计算价钱
                if (ck_chose.isChecked()) {
                    PriceAndCountEvent priceAndCountEvent = computer();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        //减号
        iv_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = listBean.getNum();
                if (num == 1) {
                    return;
                }
                tv_commodity_show_num.setText(--num + "");
                listBean.setNum(num);
                if (ck_chose.isChecked()) {
                    PriceAndCountEvent priceAndCountEvent = computer();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });

        return inflate;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    //下面完事


    //改变二级列表的checkbox的状态   如果一级选中,控制二级也选中
    private void changeChildCbState(int groupPosition, boolean flag) {
        List<Carbean.DataBean.ListBean> listBeen = data2.get(groupPosition);
        for (int j = 0; j < listBeen.size(); j++) {
            Carbean.DataBean.ListBean listBean = listBeen.get(j);
            listBean.setChecked(flag);
        }
    }

    //判断一级列表是否全部选中
    public boolean isAllGroupCbSelect() {
        for (int i = 0; i < data2.size(); i++) {
            Carbean.DataBean dataBean = data1.get(i);
            if (!dataBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    //改变全选的状态
    private void changeAllCbState(boolean flag) {
        MessageEvent messageEvent = new MessageEvent();
        messageEvent.setCheckd(flag);
        EventBus.getDefault().post(messageEvent);
    }

    //改变一级列表的checkbox的状态
    private void changeGroupCbState(int i, boolean flag) {
        Carbean.DataBean dataBean = data1.get(i);
        dataBean.setChecked(flag);
    }

    //判断二级列表是否全部选中
    private boolean isAllChildCbSelected(int i) {
        List<Carbean.DataBean.ListBean> listBeen = data2.get(i);
        for (int j = 0; j < listBeen.size(); j++) {
            Carbean.DataBean.ListBean listBean = listBeen.get(j);
            if (!listBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    //设置全选,反选
    public void changeAllListCbState(boolean flag) {
        for (int i = 0; i < data2.size(); i++) {
            changeGroupCbState(i, flag);
            changeChildCbState(i, flag);
        }
        //算钱
        EventBus.getDefault().post(computer());
        notifyDataSetChanged();
    }
    private PriceAndCountEvent computer() {
        int count = 0;
        int price = 0;
        int to = 0;
        for (int i = 0; i < data2.size(); i++) {
            List<Carbean.DataBean.ListBean> listBeen = data2.get(i);
            for (int j = 0; j < listBeen.size(); j++) {
                Carbean.DataBean.ListBean listBean = listBeen.get(j);
                if (listBean.isChecked()) {
                    price += listBean.getNum() * listBean.getPrice();
                    count += listBean.getNum();
                    to += listBean.getNum();
                }
            }
        }
        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        priceAndCountEvent.setTo(to);
        return priceAndCountEvent;
    }
}

//
compile 'com.jakewharton:butterknife:8.8.1'
    compile 'com.android.support:recyclerview-v7:26.+'
    compile 'com.jcodecraeer:xrecyclerview:1.2.0'
    compile 'org.greenrobot:eventbus:3.1.1'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.facebook.fresco:fresco:0.13.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'io.reactivex.rxjava2:rxjava:2.1.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.squareup.retrofit2:retrofit:2.0.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值