购物车练习 依赖 implementation 'com.squareup.okio:okio:1.5.0'

依赖

implementation 'com.squareup.okio:okio:1.5.0'
implementation 'com.squareup.okhttp3:okhttp:3.2.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.google.code.gson:gson:2.2.4'


compile 'io.reactivex:rxjava:x.y.z'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'com.github.bumptech.glide:glide:3.7.0'

权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.asus.myapplication.view.MainActivity">

    <ExpandableListView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_expand">
    </ExpandableListView>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="#fff">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/all_check"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/price"
            android:text="0.00"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/jiesuan"
            android:background="#f00"
            android:text="结算"
            android:padding="15dp"/>
    </LinearLayout>

</RelativeLayout>
add_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/child_image"
    android:layout_marginRight="5dp"
    android:layout_toLeftOf="@+id/child_text_delete"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/child_text_jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cart_biankuang"
        android:padding="5dp"
        android:text="-"/>

    <TextView
        android:id="@+id/child_text_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cart_biankuang"
        android:paddingBottom="5dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="5dp"/>

    <TextView
        android:id="@+id/child_text_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/cart_biankuang"
        android:padding="5dp"
        android:text="+"/>

</LinearLayout>

child_item.xml

<?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/cbProduct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"/>

    <TextView
        android:id="@+id/tvDel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:background="#ff0000"
        android:gravity="center_vertical"
        android:text="删除"
        android:textColor="#ffffff"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/tvDel"
        android:layout_toRightOf="@id/cbProduct"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center_vertical"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="12364789"
                android:layout_marginTop="5dp"/>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >

                <TextView
                    android:id="@+id/tvPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:text="12312"
                    android:textColor="#ff0000"/>

                <com.example.asus.myapplication.utils.AddSubView
                    android:id="@+id/addSubCard"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"></com.example.asus.myapplication.utils.AddSubView>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

group_iteb.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="30dp"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/cbSeller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"/>

    <TextView
        android:id="@+id/tvSeller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"/>

</LinearLayout>

shape 

cart_biankuang.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff" />

    <stroke
        android:width="0.1dp"
        android:color="#000000" />

</shape>
JAVA

Api包

Api

package com.example.asus.myapplication.Api;

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

public interface Api {
    String UPDATECARTS_URL="https://www.zhaoapi.cn/product/updateCarts";
    String GETCARTS_URL="https://www.zhaoapi.cn/product/getCarts";
    String DELETECART_URL = "https://www.zhaoapi.cn/product/deleteCart";
}
bean包

GetCartsBean

package com.example.asus.myapplication.bean;

import java.util.List;

public class GetCartsBean {

    /**
     * msg : 请求成功
     * code : 0
     * data : [{"list":[{"bargainPrice":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd
     * .com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term
     * =QQfriends","images":"https://m.360buyimg
     * .com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg
     * .com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg
     * .com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":2,"pid":46,"price":234,
     * "pscid":39,"selected":1,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus
     * (A1864) 64GB 金色 移动联通电信4G手机"},{"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":7,"pid":58,"price":6399,
     * "pscid":40,"selected":1,"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":1,"sellerid":3,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味
     * 碧根果225g/袋"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":22.9,
     * "createtime":"2017-10-14T21:38:26","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":2,"pid":27,"price":488,
     * "pscid":2,"selected":1,"sellerid":4,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味
     * 碧根果225g/袋"},{"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":2,"pid":60,"price":13888,"pscid":40,"selected":1,"sellerid":4,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)",
     * "title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家4","sellerid":"4"},
     * {"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":31,"price":788,
     * "pscid":2,"selected":1,"sellerid":8,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味
     * 碧根果225g/袋"},{"bargainPrice":3455,"createtime":"2017-10-03T23:53:28","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":52,"price":666,
     * "pscid":39,"selected":1,"sellerid":8,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机
     * 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家8","sellerid":"8"},{"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":1,"pid":67,"price":14000,
     * "pscid":40,"selected":1,"sellerid":11,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook
     * Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家11","sellerid":"11"},{"list":[{"bargainPrice":111.99,
     * "createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd
     * .com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term
     * =QQfriends","images":"https://m.360buyimg
     * .com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg
     * .com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg
     * .com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":20,"price":599,
     * "pscid":1,"selected":1,"sellerid":13,
     * "subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼
     * 老北京月饼礼盒655g"}],"sellerName":"商家13","sellerid":"13"},{"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":1,"price":118,
     * "pscid":1,"selected":1,"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":2,"pid":2,"price":299,
     * "pscid":1,"selected":1,"sellerid":18,
     * "subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼
     * 老北京月饼礼盒655g"},{"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":1,"pid":74,"price":35999,"pscid":40,"selected":1,"sellerid":18,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)",
     * "title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"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":7,"pid":75,"price":36999,
     * "pscid":40,"selected":1,"sellerid":19,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook
     * Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家19","sellerid":"19"},{"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":2,"pid":6,"price":7.99,
     * "pscid":1,"selected":1,"sellerid":22,
     * "subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼
     * 老北京月饼礼盒655g"},{"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":1,"sellerid":22,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)",
     * "title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"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":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd
         * .com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term
         * =QQfriends","images":"https://m.360buyimg
         * .com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg
         * .com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg
         * .com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":2,"pid":46,
         * "price":234,"pscid":39,"selected":1,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样",
         * "title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"},{"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":7,"pid":58,
         * "price":6399,"pscid":40,"selected":1,"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
         */
        private String sellerName;
        private String sellerid;
        private List<ListBean> 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 {
            /**
             * bargainPrice : 6666.0
             * createtime : 2017-10-10T16:01:31
             * detailUrl : https://item.m.jd.com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm
             * #_campaign=t#_335139774&utm#_term=QQfriends
             * images : https://m.360buyimg.com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg
             * !q70.jpg|https://m.360buyimg.com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c
             * .jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db
             * .jpg!q70.jpg
             * num : 2
             * pid : 46
             * price : 234.0
             * pscid : 39
             * selected : 1
             * sellerid : 2
             * subhead : 【iPhone新品上市】新一代iPhone,让智能看起来更不一样
             * title : Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机
             */

            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;
            }
        }
    }
}
SellerBean
package com.example.asus.myapplication.bean;

public class SellerBean {
    private String sellerName;
    private String sellerid;
    private boolean selected;//商家是否选中

    public boolean isSelected() {
        return selected;
    }

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

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

net包

MyInterceptor

package com.example.asus.myapplication.net;


import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class MyInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response;
        //区分是GET请求还是POST请求
        Request originRequest = chain.request();
        if ("GET".equals(originRequest.method())) {
            HttpUrl httpUrl = originRequest.url()
                    .newBuilder()
                    .addQueryParameter("source", "android")
                    .build();
            Request request = new Request.Builder().url(httpUrl).build();
            //发送请求
            response = chain.proceed(request);
        } else {
            FormBody.Builder builder = new FormBody.Builder();

            FormBody body = (FormBody) originRequest.body();
            int size = body.size();
            for (int i = 0; i < size; i++) {
                builder.add(body.name(i), body.value(i));
            }
            builder.add("source", "android");
            FormBody formBody = builder.build();
            Request request = new Request.Builder()
                    .url(originRequest.url())
                    .post(formBody)
                    .build();
            response = chain.proceed(request);

        }
        return response;
    }
}

OkHttpUtils

package com.example.asus.myapplication.net;

import android.os.Handler;
import android.os.Looper;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

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

public class OkHttpUtils {
    private static OkHttpUtils instance;
    private final OkHttpClient okHttpClient;
    private final Handler handler;

    public OkHttpUtils (){
        okHttpClient = new OkHttpClient.Builder()
                .readTimeout(10, TimeUnit.SECONDS)//读取超时时间
                .writeTimeout(10, TimeUnit.SECONDS)//写入超时时间
                .connectTimeout(10, TimeUnit.SECONDS)//连接超时时间
                .addInterceptor(new MyInterceptor())//拦截器
                .build();
        handler = new Handler(Looper.getMainLooper());
    }
    public static  OkHttpUtils getinstance (){
        if (instance==null){
            instance=new OkHttpUtils();
        }
        return instance;
    }
    public void dopost(String url, Map<String,String> params, final OnNettListener onNettListener){
        if (params==null){
            throw new RuntimeException("params is NULL!!!");
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String,String> entry:params.entrySet()){
            builder.add(entry.getKey(),entry.getValue());
        }
        //创建FormBody
        FormBody formBody = builder.build();
        //创建Request
        Request request = new Request.Builder().url(url).post(formBody).build();
        //请求数据
        okHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(okhttp3.Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNettListener.onFailed(e);
                    }
                });
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                //拿到服务器返回的数据
                final String string = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNettListener.onSuccess(string);
                    }
                });
            }


        });
    }
    public void doGet(String url, final OnNettListener onNetListener) {
        //创建Request
        final Request request = new Request.Builder()
                .url(url)
                .build();
        //发送请求
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(okhttp3.Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onFailed(e);
                    }
                });
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                //拿到服务器返回的数据
                final String string = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(string);
                    }
                });
            }


        });
    }
}

OnNettListener

package com.example.asus.myapplication.net;



public interface OnNettListener {
    void onSuccess(String str) ;

    void onFailed(Exception e);
}
utils包

AddSubView

package com.example.asus.myapplication.utils;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.asus.myapplication.R;
public class AddSubView extends LinearLayout {

    private TextView sub;
    private TextView num;
    private TextView add;

    public AddSubView(Context context) {
        this(context, null);
    }

    public AddSubView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.add_view, this);
        sub = findViewById(R.id.child_text_jian);
        num = findViewById(R.id.child_text_num);
        add = findViewById(R.id.child_text_add);
    }

    /**
     * 设置数量
     * @param str
     */
    public void setNum(String str) {
        num.setText(str);
    }

    /**
     * 获取数量
     *
     * @return
     */
    public String getNum() {
        return num.getText().toString();
    }

    /**
     * 给加号设置点击事件
     *
     * @param onclickListener
     */
    public void setAddOnclickListener(OnClickListener onclickListener) {
        add.setOnClickListener(onclickListener);
    }

    /**
     * 给减号设置点击事件
     *
     * @param onclickListener
     */
    public void setSubOnclickListener(OnClickListener onclickListener) {
        sub.setOnClickListener(onclickListener);
    }
}

DialogUtil

package com.example.asus.myapplication.utils;

import android.app.ProgressDialog;
import android.content.Context;

public class DialogUtil {

    public static ProgressDialog getProgressDialog(Context context) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("正在加载...");
        progressDialog.setCancelable(false);
        return progressDialog;
    }

}


FlowLayout

package com.example.asus.myapplication.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 流布局
 */
public class FlowLayout extends ViewGroup {
    /**
     * 存储所有的View
     */
    private List<List<View>>    mAllViews   = new ArrayList<List<View>>();
    /**
     * 每一行的高度
     */
    private List<Integer>       mLineHeight = new ArrayList<Integer>();
    public FlowLayout(Context context) {
        this(context, null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取测量模式和系统指定的宽高
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        //定义最终要显示宽高
        int width = 0;
        int height = 0;

        //定义行宽、行高
        int lineWidth = 0;
        int lineHeight = 0;

        //获取有多少个子控件
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            //获取每一个控件
            View child = getChildAt(i);
            //测量每一个控件的宽高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();

            // 子View占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin
                    + lp.rightMargin;
            // 子View占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin
                    + lp.bottomMargin;

            // 换行 判断 当前的宽度大于 开辟新行
            if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) {
                // 对比得到最大的宽度
                width = Math.max(width, lineWidth);
                // 重置lineWidth
                lineWidth = childWidth;
                // 记录行高
                height += lineHeight;
                lineHeight = childHeight;
            } else
            // 未换行
            {
                // 叠加行宽
                lineWidth += childWidth;
                // 得到当前行最大的高度
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 特殊情况,最后一个控件
            if (i == childCount - 1) {
                width = Math.max(lineWidth, width);
                height += lineHeight;
            }
        }
        setMeasuredDimension(
                modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width + getPaddingLeft() + getPaddingRight(),
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop() + getPaddingBottom()//
        );
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mAllViews.clear();
        mLineHeight.clear();

        // 当前ViewGroup的宽度
        int width = getWidth();

        int lineWidth = 0;
        int lineHeight = 0;

        // 存放每一行的子view
        List<View> lineViews = new ArrayList<View>();

        int cCount = getChildCount();

        for (int i = 0; i < cCount; i++)
        {
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();

            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            // 如果需要换行
            if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() - getPaddingRight())
            {
                // 记录LineHeight
                mLineHeight.add(lineHeight);
                // 记录当前行的Views
                mAllViews.add(lineViews);

                // 重置我们的行宽和行高
                lineWidth = 0;
                lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
                // 重置我们的View集合
                lineViews = new ArrayList<View>();
            }
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
                    + lp.bottomMargin);
            lineViews.add(child);

        }// for end
        // 处理最后一行
        mLineHeight.add(lineHeight);
        mAllViews.add(lineViews);

        // 设置子View的位置

        int left = getPaddingLeft();
        int top = getPaddingTop();

        // 行数
        int lineNum = mAllViews.size();

        for (int i = 0; i < lineNum; i++)
        {
            // 当前行的所有的View
            lineViews = mAllViews.get(i);
            lineHeight = mLineHeight.get(i);

            for (int j = 0; j < lineViews.size(); j++)
            {
                View    child = lineViews.get(j);
                // 判断child的状态
                if (child.getVisibility() == View.GONE)
                {
                    continue;
                }

                MarginLayoutParams lp = (MarginLayoutParams) child
                        .getLayoutParams();

                int lc = left + lp.leftMargin;
                int tc = top + lp.topMargin;
                int rc = lc + child.getMeasuredWidth();
                int bc = tc + child.getMeasuredHeight();

                // 为子View进行布局
                child.layout(lc, tc, rc, bc);

                left += child.getMeasuredWidth() + lp.leftMargin
                        + lp.rightMargin;
            }
            left = getPaddingLeft();
            top += lineHeight;
        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
}
model包

DeleteCartModel

package com.example.asus.myapplication.model;


import com.example.asus.myapplication.net.OnNettListener;

/**
 * Created by asus on 2018/4/28.
 */

public interface DeleteCartModel {
    void deleteCart(String uid, String pid, String token, OnNettListener onNettListener);
}

DeleteCartModelImp

package com.example.asus.myapplication.model;

import com.example.asus.myapplication.Api.Api;
import com.example.asus.myapplication.net.OkHttpUtils;
import com.example.asus.myapplication.net.OnNettListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by asus on 2018/4/28.
 */

public class DeleteCartModelImp implements DeleteCartModel {


    @Override
    public void deleteCart(String uid, String pid, String token, final OnNettListener onNettListener) {
        Map<String,String> params = new HashMap<>();
        params.put("uid",uid);
        params.put("pid",pid);
        params.put("token",token);
        OkHttpUtils.getinstance().dopost(Api.DELETECART_URL, params, new OnNettListener() {
            @Override
            public void onSuccess(String result) {
                onNettListener.onSuccess(result);
            }

            @Override
            public void onFailed(Exception e) {
                onNettListener.onFailed(e);
            }
        });
    }
}

GetCartsModel

package com.example.asus.myapplication.model;


import com.example.asus.myapplication.net.OnNettListener;

/**
 * Created by asus on 2018/4/26.
 */

public interface GetCartsModel {
    void getCarts(String uid, String token, OnNettListener onNettListener);
}

GetCartsModelImp

package com.example.asus.myapplication.model;

import com.example.asus.myapplication.Api.Api;
import com.example.asus.myapplication.bean.GetCartsBean;
import com.example.asus.myapplication.net.OkHttpUtils;
import com.example.asus.myapplication.net.OnNettListener;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by asus on 2018/4/26.
 */

public class GetCartsModelImp implements GetCartsModel {
    @Override
    public void getCarts(String uid,String token, final OnNettListener onNettListener) {
        Map<String, String> params = new HashMap<>();
        params.put("uid", uid);
        params.put("token", token);
        OkHttpUtils.getinstance().dopost(Api.GETCARTS_URL, params, new OnNettListener() {
            @Override
            public void onSuccess(String str) {
                GetCartsBean getCartsBean = new Gson().fromJson(str, GetCartsBean.class);
                if ("0".equals(getCartsBean.getCode())){
                    onNettListener.onSuccess(str);
                    //Log.i("ssss",str);
                }
            }

            @Override
            public void onFailed(Exception e) {
                onNettListener.onFailed(e);
            }
        });
    }
}

UpdateCartsModel

package com.example.asus.myapplication.model;


import com.example.asus.myapplication.net.OnNettListener;

/**
 * Created by asus on 2018/4/27.
 */

public interface UpdateCartsModel {
    void updateCarts(String uid, String sellerid, String pid, String num, String selected, String token,
                     OnNettListener onNettListener);
}

UpdateCartsModelImp

package com.example.asus.myapplication.model;

import com.example.asus.myapplication.Api.Api;
import com.example.asus.myapplication.net.OkHttpUtils;
import com.example.asus.myapplication.net.OnNettListener;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by asus on 2018/4/27.
 */

public class UpdateCartsModelImp implements UpdateCartsModel {
    @Override
    public void updateCarts(String uid, String sellerid, String pid, String num, String selected, String token, final OnNettListener onNetListener) {
        Map<String,String> params = new HashMap<>();
        params.put("uid",uid);
        params.put("sellerid",sellerid);
        params.put("pid",pid);
        params.put("num",num);
        params.put("selected",selected);
        params.put("token",token);
        OkHttpUtils.getinstance().dopost(Api.UPDATECARTS_URL, params, new OnNettListener() {
            @Override
            public void onSuccess(String result) {
//                new Gson().fromJson(result,)
                onNetListener.onSuccess(result);
            }

            @Override
            public void onFailed(Exception e) {

            }
        });
    }
}
presenter包

DeleteCartPresenter

package com.example.asus.myapplication.presenter;

/**
 * Created by asus on 2018/4/28.
 */

public interface DeleteCartPresenter {
    void deleteCart(String uid, String pid, String token);
}

DeleteCartPresenterImp

package com.example.asus.myapplication.presenter;

import com.example.asus.myapplication.model.DeleteCartModelImp;
import com.example.asus.myapplication.net.OnNettListener;
import com.example.asus.myapplication.view.DeleteCartView;

/**
 * Created by asus on 2018/4/28.
 */

public class DeleteCartPresenterImp  implements DeleteCartPresenter {

    private final DeleteCartModelImp deleteCartModelImp;
    private DeleteCartView deleteCartView;

    public DeleteCartPresenterImp(DeleteCartView deleteCartView) {
        this.deleteCartView = deleteCartView;
        deleteCartModelImp = new DeleteCartModelImp();
    }

    @Override
    public void deleteCart(String uid, String pid, String token) {
        deleteCartModelImp.deleteCart(uid, pid, token, new OnNettListener() {
            @Override
            public void onSuccess(String result) {
                deleteCartView.delSuccess();
            }

            @Override
            public void onFailed(Exception e) {

            }
        });

    }
}

GetCartsPresenter

package com.example.asus.myapplication.presenter;

/**
 * Created by asus on 2018/4/26.
 */

public interface GetCartsPresenter {
    void getCarts(String uid, String token);
}

GetCartsPresenterImp

package com.example.asus.myapplication.presenter;

import android.util.Log;

import com.example.asus.myapplication.bean.GetCartsBean;
import com.example.asus.myapplication.bean.SellerBean;
import com.example.asus.myapplication.model.GetCartsModelImp;
import com.example.asus.myapplication.net.OnNettListener;
import com.example.asus.myapplication.view.GetCartsView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2018/4/26.
 */

public class GetCartsPresenterImp implements GetCartsPresenter {
    private GetCartsView getCartsView;
    private final GetCartsModelImp getCartsModelImp;

    public GetCartsPresenterImp(GetCartsView getCartsView) {
        this.getCartsView = getCartsView;
        getCartsModelImp = new GetCartsModelImp();
    }

    @Override
    public void getCarts(String uid,String token ) {
        getCartsModelImp.getCarts(uid,token, new OnNettListener() {
            @Override
            public void onSuccess(String str) {
                Log.e("dddd",str);
                if (getCartsView != null) {
                    List<SellerBean> groupList = new ArrayList<>();//用于封装一级列表
                    List<List<GetCartsBean.DataBean.ListBean>> childList = new ArrayList<>();//用于封装二级列表

                    GetCartsBean getCartsBean = new Gson().fromJson(str, GetCartsBean.class);
                    List<GetCartsBean.DataBean> data = getCartsBean.getData();

                    for (int i = 0; i < data.size(); i++) {
                        GetCartsBean.DataBean dataBean = data.get(i);
                        SellerBean sellerBean = new SellerBean();
                        sellerBean.setSellerName(dataBean.getSellerName());
                        sellerBean.setSellerid(dataBean.getSellerid());
                        sellerBean.setSelected(isSellerProductAllSelect(dataBean));
                        groupList.add(sellerBean);

                        List<GetCartsBean.DataBean.ListBean> list = dataBean.getList();
                        childList.add(list);
                    }
                    getCartsView.showData(groupList, childList);
                }
            }

            @Override
            public void onFailed(Exception e) {

            }
        });
    }
    /**
     * 判断该商家下面的商品是否全选
     *
     * @return
     */
    private boolean isSellerProductAllSelect(GetCartsBean.DataBean dataBean) {
        //获取该商家下面的所有商品
        List<GetCartsBean.DataBean.ListBean> list = dataBean.getList();
        for (int i = 0; i < list.size(); i++) {
            GetCartsBean.DataBean.ListBean listBean = list.get(i);
            if (0 == listBean.getSelected()) {
                //如果是0的话,表示有一个商品未选中
                return false;
            }
        }
        return true;

    }
    public void detach() {
        if (getCartsView != null) {
            getCartsView = null;
        }
    }
}

UpdateCartsPresenter

package com.example.asus.myapplication.presenter;

/**
 * Created by asus on 2018/4/27.
 */

public interface UpdateCartsPresenter {
    void updateCarts(String uid, String sellerid, String pid, String num, String selected, String token);
}

UpdateCartsPresenterImp

package com.example.asus.myapplication.presenter;


import com.example.asus.myapplication.model.UpdateCartsModelImp;
import com.example.asus.myapplication.net.OnNettListener;
import com.example.asus.myapplication.view.UpdateCartsView;

/**
 * Created by asus on 2018/4/27.
 */

public class UpdateCartsPresenterImp implements UpdateCartsPresenter {
    private final UpdateCartsModelImp updateCartsModelImp;
    private UpdateCartsView updateCartsView;

    public UpdateCartsPresenterImp(UpdateCartsView updateCartsView) {
        this.updateCartsView = updateCartsView;
        updateCartsModelImp = new UpdateCartsModelImp();
    }


    @Override
    public void updateCarts(String uid, String sellerid, String pid, String num, String selected,String token) {
        updateCartsModelImp.updateCarts(uid, sellerid, pid, num, selected,token, new OnNettListener() {
            @Override
            public void onSuccess(String result) {
                updateCartsView.updataSuccess();
            }

            @Override
            public void onFailed(Exception e) {

            }
        });
    }
}

view包

DeleteCartView

package com.example.asus.myapplication.view;

/**
 * Created by asus on 2018/4/28.
 */

public interface DeleteCartView {
    void delSuccess();
}

GetCartsView

package com.example.asus.myapplication.view;

import com.example.asus.myapplication.bean.GetCartsBean;
import com.example.asus.myapplication.bean.SellerBean;

import java.util.List;

/**
 * Created by asus on 2018/4/26.
 */

public interface GetCartsView {
    void showData(List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>> childList);
}

MainActivity

package com.example.asus.myapplication.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.example.asus.myapplication.R;
import com.example.asus.myapplication.adapter.ElvShopcartAdapter;
import com.example.asus.myapplication.bean.GetCartsBean;
import com.example.asus.myapplication.bean.SellerBean;
import com.example.asus.myapplication.presenter.GetCartsPresenterImp;

import java.util.List;

public class MainActivity extends AppCompatActivity implements GetCartsView {

    private ExpandableListView mMyExpand;
    private CheckBox mAllCheck;
    /**
     * 0.00
     */
    private TextView mPrice;
    /**
     * 结算
     */
    private TextView mJiesuan;
    private GetCartsPresenterImp getCartsPresenterImp;

    private final String token = "10401FFDCD75934C322C4A5C81931740";
    private ElvShopcartAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getCartsPresenterImp = new GetCartsPresenterImp(this);

        getCartsPresenterImp.getCarts("12879",token);
        mAllCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.changeAllState(mAllCheck.isChecked());
            }
        });
    }

    private void initView() {
        mMyExpand = (ExpandableListView) findViewById(R.id.my_expand);
        mAllCheck = (CheckBox) findViewById(R.id.all_check);
        mPrice = (TextView) findViewById(R.id.price);
        mJiesuan = (TextView) findViewById(R.id.jiesuan);
    }

    @Override
    public void showData(List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>> childList) {
        //判断所有商家是否全部选中
        mAllCheck.setChecked(isSellerAddSelected(groupList));
        adapter = new ElvShopcartAdapter(this, groupList, childList,getCartsPresenterImp);
        mMyExpand.setAdapter(adapter);
        //获取数量和总价
        String[] strings = adapter.computeMoneyAndNum();
        mPrice.setText("总计:" + strings[0] + "元");
        mJiesuan.setText("去结算("+strings[1]+"个)");
        for (int i=0;i<groupList.size();i++){
            mMyExpand.expandGroup(i);
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        getCartsPresenterImp.detach();
    }
    /**
     * 判断所有商家是否全部选中
     *
     * @param groupList
     * @return
     */
    private boolean isSellerAddSelected(List<SellerBean> groupList) {
        for (int i = 0; i < groupList.size(); i++) {
            SellerBean sellerBean = groupList.get(i);
            if (!sellerBean.isSelected()) {
                return false;
            }
        }
        return true;
    }
}

UpdateCartsView

package com.example.asus.myapplication.view;

/**
 * Created by asus on 2018/4/27.
 */

public interface UpdateCartsView {
    void updataSuccess();
}

adapter包

ElvShopcartAdapter

package com.example.asus.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.asus.myapplication.R;
import com.example.asus.myapplication.bean.GetCartsBean;
import com.example.asus.myapplication.bean.SellerBean;
import com.example.asus.myapplication.presenter.DeleteCartPresenterImp;
import com.example.asus.myapplication.presenter.GetCartsPresenterImp;
import com.example.asus.myapplication.presenter.UpdateCartsPresenterImp;
import com.example.asus.myapplication.utils.AddSubView;
import com.example.asus.myapplication.view.DeleteCartView;
import com.example.asus.myapplication.view.UpdateCartsView;

import java.util.List;

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

public class ElvShopcartAdapter extends BaseExpandableListAdapter implements UpdateCartsView ,DeleteCartView{
    private Context context;
    private List<SellerBean> groupList;
    private List<List<GetCartsBean.DataBean.ListBean>> childList;
    private LayoutInflater inflater;
    private final UpdateCartsPresenterImp updateCartsPresenterImp;
    private GetCartsPresenterImp getCartsPresenterImp;
    private final String uid = "12879";
    private final String token = "10401FFDCD75934C322C4A5C81931740";
   // private ProgressDialog progressDialog;
    private int productIndex;
    private int groupPosition;
    private boolean checked;
    private static final int GETCARTS = 0;//查询购物车
    private static final int UPDATE_PRODUCT = 1; //更新商品
    private static final int UPDATE_SELLER = 2; //更新卖家

    private static int state = GETCARTS;
    private boolean allSelected;
    private final DeleteCartPresenterImp deleteCartPresenterImp;

    public ElvShopcartAdapter(Context context, List<SellerBean> groupList, List<List<GetCartsBean.DataBean.ListBean>>
            childList, GetCartsPresenterImp getCartsPresenterImp ) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
        inflater = LayoutInflater.from(context);
        this.getCartsPresenterImp = getCartsPresenterImp;

        //绑定
        updateCartsPresenterImp = new UpdateCartsPresenterImp(this);
        deleteCartPresenterImp = new DeleteCartPresenterImp(this);


    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final GroupViewHolder groupViewHolder;
        if (convertView == null) {
            groupViewHolder = new GroupViewHolder();
            convertView = inflater.inflate(R.layout.group_iteb, null);
            groupViewHolder.cbSeller = convertView.findViewById(R.id.cbSeller);
            groupViewHolder.tvSeller = convertView.findViewById(R.id.tvSeller);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        //设置值
        final SellerBean sellerBean = groupList.get(groupPosition);
        groupViewHolder.tvSeller.setText(sellerBean.getSellerName());
        groupViewHolder.cbSeller.setChecked(sellerBean.isSelected());

        //给商家checkbox设置点击事件
        groupViewHolder.cbSeller.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置当前的更新状态
                state = UPDATE_PRODUCT;
                //显示进度条
               // progressDialog.show();
                //默认从第一个商品开始更新购物车状态
                productIndex = 0;
                //全局记录一下当前更新的商家
                ElvShopcartAdapter.this.groupPosition = groupPosition;
                //该商家是否选中
                checked = groupViewHolder.cbSeller.isChecked();
                //更新商家下的商品状态
                updateProductInSeller();
            }

        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             ViewGroup
                                     parent) {

        final ChildViewHolder childViewHolder;
        if (convertView == null) {
            childViewHolder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.child_item, null);
            childViewHolder.cbProduct = convertView.findViewById(R.id.cbProduct);
            childViewHolder.iv = convertView.findViewById(R.id.iv);
            childViewHolder.tvTitle = convertView.findViewById(R.id.tvTitle);
            childViewHolder.tvPrice = convertView.findViewById(R.id.tvPrice);
            childViewHolder.tvDel = convertView.findViewById(R.id.tvDel);
            childViewHolder.addSubView = convertView.findViewById(R.id.addSubCard);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        final GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(childPosition);
        //根据服务器返回的select值,给checkBox设置是否选中
        childViewHolder.cbProduct.setChecked(listBean.getSelected() == 1 ? true : false);
        childViewHolder.tvTitle.setText(listBean.getTitle());
        childViewHolder.tvPrice.setText(listBean.getPrice() + "");
        Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(childViewHolder.iv);
        childViewHolder.addSubView.setNum(listBean.getNum() + "");
        //给二级列表的checkbox设置点击事件
        childViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                state = GETCARTS;
                //显示进度条
               // progressDialog.show();
                ElvShopcartAdapter.this.groupPosition = groupPosition;
                //调用更新购物车接口,改变购物车的状态
                //获取卖家id
                String sellerid = groupList.get(groupPosition).getSellerid();
                //获取pid
                String pid = listBean.getPid() + "";
                //是否选中
                boolean childChecked = childViewHolder.cbProduct.isChecked();

                updateCartsPresenterImp.updateCarts(uid, sellerid, pid, "1", childChecked ? "1" : "0", token);
            }
        });

        //给加号设置点击事件
        childViewHolder.addSubView.setAddOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // progressDialog.show();
                state = GETCARTS;
                //获取sellerId
                String sellerid = groupList.get(groupPosition).getSellerid();
                //获取pid
                int pid = listBean.getPid();
                //获取数量
                int num = listBean.getNum();
                num += 1;
                //是否选中
                String isChecked = childViewHolder.cbProduct.isChecked() ? "1" : "0";
                //调用更新购物车的接口即可
                updateCartsPresenterImp.updateCarts(uid, sellerid, pid + "", num + "", isChecked, token);
            }
        });

        //给减号设置点击事件
        childViewHolder.addSubView.setSubOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //   progressDialog.show();
                state = GETCARTS;
                //获取当前商品的数量
                int num = listBean.getNum();
                if (num <= 1) {
                   // progressDialog.dismiss();
                    Toast.makeText(context, "数量不能小于1", Toast.LENGTH_SHORT).show();
                    return;
                }
                num -= 1;

                //获取sellerId
                String sellerid = groupList.get(groupPosition).getSellerid();
                //获取pid
                int pid = listBean.getPid();
                //是否选中
                String isChecked = childViewHolder.cbProduct.isChecked() ? "1" : "0";
                //更新购物车
                updateCartsPresenterImp.updateCarts(uid, sellerid, pid + "", num + "", isChecked, token);
            }
        });

        //给删除设置点击事件
        childViewHolder.tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                progressDialog.show();
                state = GETCARTS;
                //获取pid
                int pid = listBean.getPid();
                //删除购物车里的选项
                deleteCartPresenterImp.deleteCart(uid, pid + "", token);

            }
        });

        return convertView;
    }

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

    //删除成功回调接口
    @Override
    public void delSuccess() {
        getCartsPresenterImp.getCarts(uid, token);
    }


    class GroupViewHolder {
        CheckBox cbSeller;
        TextView tvSeller;
    }

    class ChildViewHolder {
        CheckBox cbProduct;
        ImageView iv;
        TextView tvTitle;
        TextView tvPrice;
        TextView tvDel;
        AddSubView addSubView;
    }

    //更新购物车成功回调的方法
    @Override
    public void updataSuccess() {
        switch (state) {
            case GETCARTS:
                //更新成功以后调用查询购物车接口
                productIndex = 0;
                groupPosition = 0;
                getCartsPresenterImp.getCarts(uid, token);
                break;
            case UPDATE_PRODUCT:
                //更新成功一个商品以后,再接着更新该商家下面的其它商品,直到没有商品为止
                productIndex++;
                //下标是否越界
                if (productIndex < childList.get(groupPosition).size()) {
                    //可以继续跟新商品
                    updateProductInSeller();
                } else {
                    //商品已经全部更新完成,请查询购物车
                    state = GETCARTS;
                    updataSuccess();
                }
                break;
            case UPDATE_SELLER:
                //遍历所有商家下的商品,并更新状态
                productIndex++;
                //下标是否越界
                if (productIndex < childList.get(groupPosition).size()) {
                    //可以继续跟新商品
                    updateProductInSeller(allSelected);
                } else {
                    //商品已经全部更新完成,请查询购物车
                    productIndex = 0;
                    groupPosition++;
                    if (groupPosition < groupList.size()) {
                        //可以继续跟新商品
                        updateProductInSeller(allSelected);
                    } else {
                        //商品已经全部更新完成,请查询购物车
                        state = GETCARTS;
                        updataSuccess();
                    }
                }
                break;
        }

    }

    private void updateProductInSeller() {
        //获取SellerId
        SellerBean sellerBean = groupList.get(groupPosition);
        String sellerid = sellerBean.getSellerid();
        //获取pid
        GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(productIndex);
        int num = listBean.getNum();
        int pid = listBean.getPid();
        updateCartsPresenterImp.updateCarts(uid, sellerid, pid + "", num + "", checked ? "1" : "0", token);
    }

    private void updateProductInSeller(boolean bool) {
        //获取SellerId
        SellerBean sellerBean = groupList.get(groupPosition);
        String sellerid = sellerBean.getSellerid();
        //获取pid
        GetCartsBean.DataBean.ListBean listBean = childList.get(groupPosition).get(productIndex);
        int pid = listBean.getPid();
        int num = listBean.getNum();
        updateCartsPresenterImp.updateCarts(uid, sellerid, pid + "", num + "", bool ? "1" : "0", token);
    }

    /**
     * 计算数量和价钱
     *
     * @return
     */
    public String[] computeMoneyAndNum() {
        double sum = 0;
        int num = 0;
        for (int i = 0; i < groupList.size(); i++) {
            for (int j = 0; j < childList.get(i).size(); j++) {
                //判断商品是否选中
                GetCartsBean.DataBean.ListBean listBean = childList.get(i).get(j);
                if (listBean.getSelected() == 1) {
                    //该商品为选中状态
                    sum += listBean.getPrice() * listBean.getNum();
                    num += listBean.getNum();
                }
            }
        }
        return new String[]{sum + "", num + ""};
    }

    public void changeAllState(boolean bool) {
        this.allSelected = bool;
        state = UPDATE_SELLER;
        //遍历商家下的商品,修改状态
        updateProductInSeller(bool);

    }


}

猜你喜欢

转载自blog.csdn.net/frimary/article/details/80182995