一级列表的购物车

来源:互联网 发布:淘宝卡 编辑:程序博客网 时间:2024/03/29 14:27

//先把布局加载出来

MainActivity

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="test.bwei.com.gouwuche666.MainActivity">    <android.support.v7.widget.RecyclerView        android:id="@+id/r1_review"        android:layout_above="@+id/b"        android:layout_width="match_parent"        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>    <RelativeLayout        android:id="@+id/b"        android:padding="10dp"        android:layout_alignParentBottom="true"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <CheckBox            android:id="@+id/selectall"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/j"            android:layout_width="wrap_content"            android:padding="10dp"            android:text="价钱:¥"            android:textColor="#000"            android:layout_toRightOf="@+id/selectall"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/sumnoney"            android:layout_width="wrap_content"            android:padding="6dp"            android:text="money"            android:textSize="17dp"            android:textColor="#c00"            android:layout_toRightOf="@+id/j"            android:layout_height="wrap_content" />    </RelativeLayout></RelativeLayout>

//childitem

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <CheckBox    android:id="@+id/good_cb"    android:layout_centerVertical="true"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />    <ImageView        android:id="@+id/good_iv"        android:layout_toRightOf="@+id/good_cb"        android:src="@mipmap/ic_launcher"        android:layout_width="80dp"        android:layout_height="80dp" />    <TextView        android:id="@+id/good_shop"        android:padding="6dp"        android:layout_toRightOf="@+id/good_iv"        android:text="店名"        android:textSize="15dp"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/buy_content"        android:text="context"        android:layout_below="@+id/good_shop"        android:layout_toRightOf="@+id/good_iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:textColor="#c00"        android:id="@+id/good_money"        android:text="context"        android:layout_below="@+id/buy_content"        android:layout_toRightOf="@+id/good_iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/good_j"        android:text="X "        android:layout_marginLeft="80dp"        android:layout_below="@+id/buy_content"        android:layout_toRightOf="@+id/good_money"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/good_num"        android:text="mone "        android:layout_below="@+id/buy_content"        android:layout_toRightOf="@+id/good_j"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>

//item

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <CheckBox        android:id="@+id/boss_all"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:background="#ccc"        android:layout_toRightOf="@+id/boss_all"        android:id="@+id/boss"        android:padding="10dp"        android:text="boss"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <android.support.v7.widget.RecyclerView        android:layout_below="@+id/boss"        android:id="@+id/re2"        android:layout_width="match_parent"        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></RelativeLayout>

//请求的APi地址

public class Api {    public static String UPDATA_URL="http://120.27.23.105/product/updateCarts?uid=144&sellerid=1&pid=1&selected=0&num=10";    public static String LOOK_URL="http://120.27.23.105/product/getCarts?uid=144";    public static String DELETE="http://120.27.23.105/product/deleteCart?uid=144";}

//MainActivity

public class MainActivity extends AppCompatActivity {    private RecyclerView recycle;    private CheckBox cball;    private TextView money;    private List<Bean.DataBean> data;    private MyAdapter ada;    private int kb;    private double money1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initview();        initdata();        setlistener();    }    private void setlistener() {        cball.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if(cball.isChecked()){                    for (Bean.DataBean bean : data) {                        bean.setFlag(true);                        List<Bean.DataBean.ListBean> list = bean.getList();                        for (Bean.DataBean.ListBean listBean : list) {                            listBean.setSelected(1);                        }                    }                }else{                    for (Bean.DataBean bean : data) {                        bean.setFlag(false);                        List<Bean.DataBean.ListBean> list = bean.getList();                        for (Bean.DataBean.ListBean listBean : list) {                            listBean.setSelected(0);                        }                    }                }                ada.notifyDataSetChanged();            }        });    }    private void initdata() {        NetUtils.getResult(Api.LOOK_URL, new NetUtils.CallResult() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) {                try {                    String str=response.body().string();                    Gson gson=new Gson();                    Bean bean = gson.fromJson(str, Bean.class);                    data = bean.getData();                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                            ada= new MyAdapter((ArrayList<Bean.DataBean>) data,MainActivity.this);                            recycle.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));                            recycle.setAdapter(ada);                                                        ada.setMoneyResult(new MyAdapter.MoneyResult() {                                @Override                                public void money() {                                    kb= 0;                                    money1= 0;                                    for (Bean.DataBean dataBean : data) {                                        List<Bean.DataBean.ListBean> been = dataBean.getList();                                        for (Bean.DataBean.ListBean listBean : been) {                                            if(listBean.getSelected()==1){                                                kb++;                                                money1+=listBean.getBargainPrice()*listBean.getNum();                                            }                                        }                                    }                                    //设置总价                                    money.setText(money1+"");                                }                            });                        }                    });                } catch (IOException e) {                    e.printStackTrace();                }            }        });    }    private void initview() {        recycle = (RecyclerView) findViewById(R.id.r1_review);        cball = (CheckBox) findViewById(R.id.selectall);        money = (TextView) findViewById(R.id.sumnoney);    }

//Bean

public class Bean {    /**     * msg : 请求成功     * code : 0     * data : [{"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"},{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":90,"price":1233,"pscid":112,"selected":0,"sellerid":1,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":2,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":399,"createtime":"2017-10-02T15:20:02","detailUrl":"https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg","num":1,"pid":81,"price":699,"pscid":85,"selected":0,"sellerid":2,"subhead":"2件,总价打6.50","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":2,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS )"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:53:28","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":26,"price":88,"pscid":2,"selected":0,"sellerid":3,"subhead":"三只松鼠零食特惠,专区满9950,满199100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/"}],"sellerName":"商家3","sellerid":"3"},{"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":2,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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":3,"pid":6,"price":7.99,"pscid":1,"selected":0,"sellerid":22,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家22","sellerid":"22"}]     */    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":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"},{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":90,"price":1233,"pscid":112,"selected":0,"sellerid":1,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":2,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}]         * sellerName : 商家1         * sellerid : 1         */        private String sellerName;        private String sellerid;        private List<ListBean> list;        private boolean flag = false;        public boolean isFlag() {            return flag;        }        public void setFlag(boolean flag) {            this.flag = flag;        }        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 : 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;            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;            }        }    }

//utilds   NetUtils

public class NetUtils {    public static void getResult(String url, final CallResult callResult){//        OkHttpClient client = SingleOk.getInstence()//                .connectTimeout(10, TimeUnit.SECONDS)//                .readTimeout(10, TimeUnit.SECONDS)//                .writeTimeout(10, TimeUnit.SECONDS)//                .retryOnConnectionFailure(false)//                .build();        OkHttpClient client =new OkHttpClient.Builder().build();        FormBody.Builder build=new FormBody.Builder();        RequestBody body=build.build();        Request request=new Request.Builder().post(body).url(url).build();        client.newCall(request).enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                if(callResult!=null){                    callResult.onFailure(call,e);                }            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if(callResult!=null) {                    callResult.onResponse(call, response);                }            }        });    }    public interface CallResult{        void onFailure(Call call, IOException e);      void onResponse(Call call, Response response);    }
//适配器

//大的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {    private ArrayList<Bean.DataBean> list;    private Context context;    private ChildAdapter ada;    public MyAdapter(ArrayList<Bean.DataBean> list, Context context) {        this.list = list;        this.context = context;    }    @Override    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view=View.inflate(context, R.layout.item,null);        return new MyHolder(view);    }    @Override    public void onBindViewHolder(final MyHolder holder, final int position) {        holder.title.setText(list.get(position).getSellerName());        final List<Bean.DataBean.ListBean> l = this.list.get(position).getList();        ada = new ChildAdapter((ArrayList<Bean.DataBean.ListBean>) l,context);        holder.lv.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));        holder.lv.setAdapter(ada);        //根据数据,状态改变        holder.cb.setChecked(list.get(position).isFlag());        //商家改变子商品        holder.cb.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                list.get(position).setFlag(!list.get(position).isFlag());                for (int i = 0; i < l.size(); i++) {                    if (list.get(position).isFlag()){                        l.get(i).setSelected(1);                    }else {                        l.get(i).setSelected(0);                    }                }                holder.lv.setAdapter(ada);                notifyDataSetChanged();              moneyResult.money();            }        });        //计算钱        ada.setCallMoney(new ChildAdapter.CallMoney() {            @Override            public void changeMoney() {                moneyResult.money();            }        });        //子改变商家cb        ada.setOnItemAllClick(new ChildAdapter.OnItemAllClick() {            @Override            public void all() {                list.get(position).setFlag(true);                holder.cb.setChecked(true);                notifyDataSetChanged();            }            @Override            public void noall() {                list.get(position).setFlag(false);                holder.cb.setChecked(false);                notifyDataSetChanged();            }        });    }    @Override    public int getItemCount() {        return list.size();    }    class MyHolder extends RecyclerView.ViewHolder{        private RecyclerView lv;        private TextView title;        private CheckBox cb;        public MyHolder(View itemView) {            super(itemView);            lv=itemView.findViewById(R.id.re2);            title=itemView.findViewById(R.id.boss);            cb=itemView.findViewById(R.id.boss_all);        }    }    private MoneyResult moneyResult;    public void setMoneyResult(MoneyResult moneyResult) {        this.moneyResult = moneyResult;    }    public interface MoneyResult{        void money();    }}

//小的适配器ChildAdapter

public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.MHolder> {    private ArrayList<Bean.DataBean.ListBean> list;    private Context context;    public ChildAdapter(ArrayList<Bean.DataBean.ListBean> list, Context context) {        this.list = list;        this.context = context;    }    @Override    public MHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view=View.inflate(context, R.layout.childitem,null);        return new MHolder(view);    }    @Override    public void onBindViewHolder(final MHolder holder, final int position) {        holder.title.setText(list.get(position).getSubhead());        holder.title.setMaxLines(1);        holder.money.setText(list.get(position).getBargainPrice()+"");        holder.c.setText(list.get(position).getTitle());        holder.c.setMaxLines(1);        holder.num.setText(list.get(position).getNum()+"");        String[] split = list.get(position).getImages().split("\\|");        Glide.with(context).load(split[0]).into(holder.iv);        //根据数据改变状态        if(list.get(position).getSelected()==1){            holder.cb.setChecked(true);        }else{            holder.cb.setChecked(false);        }        //子商品的点击事件        holder.cb.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if (holder.cb.isChecked()){                    holder.cb.setChecked(true);                    list.get(position).setSelected(1);                }else {                    holder.cb.setChecked(false);                    list.get(position).setSelected(0);                }                all();                notifyDataSetChanged();                callMoney.changeMoney();            }        });    }    //判断是否是全选的回调    private int sum=0;    private void all() {        for (int i = 0; i <list.size() ; i++) {            if(list.get(i).getSelected()==1){                sum++;            }else{                sum--;            }        }        if(sum==list.size()){            onItemAllClick.all();        }else{            onItemAllClick.noall();        }        sum=0;    }    private OnItemAllClick onItemAllClick;    public void setOnItemAllClick(OnItemAllClick onItemAllClick) {        this.onItemAllClick = onItemAllClick;    }    public interface OnItemAllClick{        void all();        void noall();    }    @Override    public int getItemCount() {        return list.size();    }    class MHolder extends RecyclerView.ViewHolder{        ImageView iv;        TextView money;        TextView title;        TextView c;        CheckBox cb;        TextView num;        public MHolder(View itemView) {            super(itemView);            iv=itemView.findViewById(R.id.good_iv);            money=itemView.findViewById(R.id.good_money);            title=itemView.findViewById(R.id.good_shop);            c=itemView.findViewById(R.id.buy_content);            cb=itemView.findViewById(R.id.good_cb);            num=itemView.findViewById(R.id.good_num);        }    }    private CallMoney callMoney;    public void setCallMoney(CallMoney callMoney) {        this.callMoney = callMoney;    }    public interface CallMoney{        void changeMoney();    }}


原创粉丝点击