1.添加网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
2.添加依赖
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.android.support:recyclerview-v7:27+'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okio:okio:1.11.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
3.创建布局
在values文件夹下创建 dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="left_space_pading">12dp</dimen>
</resources>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/el_cart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#eeeeee"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_care_all_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"/>
<TextView
android:id="@+id/tv_cart_total_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="20dp"
android:text="合计:¥0.00"/>
<Button
android:id="@+id/btn_cart_pay"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="去结算(0)"/>
</LinearLayout>
</LinearLayout>
add_remove_view_layput
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="30dp"
android:padding="2dp"
android:gravity="center_vertical"
android:background="#99000000"
android:layout_gravity="center_vertical"
>
<TextView
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/sub_tv"
android:gravity="center"
android:text="-"
android:textSize="16sp"
android:layout_width="0dp"
android:layout_height="match_parent" />
<TextView
android:text="1"
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/product_number_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
/>
<TextView
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/add_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textSize="16sp"
android:text="+"/>
</LinearLayout>
item_cart_parent
<?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="60dp"
android:gravity="center_vertical"
android:paddingLeft="@dimen/textandiconmargin">
<CheckBox
android:id="@+id/seller_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/seller_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
item_cart_child
<?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="120dp"
android:gravity="center_vertical"
android:paddingLeft="@dimen/left_space_pading">
<CheckBox
android:id="@+id/child_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/product_icon_iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="20dp"
android:scaleType="centerCrop"
android:src="@color/colorPrimary" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/left_space_pading"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/product_title_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="商品标题" />
<TextView
android:id="@+id/product_price_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="¥0.0" />
</LinearLayout>
<com.example.thinkpad.wsjgowu.MyAddSubView
android:id="@+id/add_remove_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
</LinearLayout>
4。创建OkhttpUtils实现封装
package com.example.thinkpad.wsjgowu;
import android.os.Handler;
import android.os.Looper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class OkhttpUtils {
private static OkhttpUtils mOkhtttpUtils;
private OkHttpClient mOkHttpClien;
private final Handler mHandler;
private OkhttpUtils() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Map<String, String> map = new HashMap<>();
map.put("source", "android");
PublicParamInterceptor publicParamInterceptor = new PublicParamInterceptor(map);
//创建一个主线程的handler
mHandler = new Handler(Looper.getMainLooper());
mOkHttpClien = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(publicParamInterceptor)
.build();
}
public static OkhttpUtils getInstance() {
if (mOkhtttpUtils == null) {
synchronized (OkhttpUtils.class) {
if (mOkhtttpUtils == null) {
return mOkhtttpUtils = new OkhttpUtils();
}
}
}
return mOkhtttpUtils;
}
public void doGet(String url, final OkCallback okCallback) {
Request request = new Request.Builder()
.get()
.url(url)
.build();
final Call call = mOkHttpClien.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (okCallback != null) {
//切换到主线程
mHandler.post(new Runnable() {
@Override
public void run() {
okCallback.onFailure(e);
}
});
}
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
try {
if (response != null && response.isSuccessful()) {
final String json = response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
if (okCallback != null) {
okCallback.onResponse(json);
return;
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void doPost(String url, Map<String, String> map, final OkCallback okCallback) {
FormBody.Builder builder = new FormBody.Builder();
if (map != null) {
for (String key : map.keySet()) {
builder.add(key, map.get(key));
}
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.post(formBody)
.url(url)
.build();
final Call call = mOkHttpClien.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (okCallback != null) {
//切换到主线程
mHandler.post(new Runnable() {
@Override
public void run() {
okCallback.onFailure(e);
}
});
}
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
try {
if (response != null && response.isSuccessful()) {
final String json = response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
if (okCallback != null) {
okCallback.onResponse(json);
return;
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
if (okCallback != null) {
okCallback.onFailure(new Exception("网络异常"));
}
}
});
}
public interface OkCallback {
void onFailure(Exception e);
void onResponse(String json);
}
//自定义一个拦截器,封装公共请求参数
public class PublicParamInterceptor implements Interceptor {
Map<String, String> paramMap = new HashMap<>();
public PublicParamInterceptor(Map<String, String> paramMap) {
this.paramMap = paramMap;
}
@Override
public Response intercept(Chain chain) throws IOException {
//拿到原来的request
Request oldRequest = chain.request();
//拿到请求的url
String url = oldRequest.url().toString();
//判断是GET还是POST请求
if (oldRequest.method().equalsIgnoreCase("GET")) {
if (paramMap != null && paramMap.size() > 0) {
StringBuilder urlBuilder = new StringBuilder(url);
//拼接公共请求参数
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
urlBuilder.append("&" + entry.getKey() + "=" + entry.getValue());
}
url = urlBuilder.toString();
//如果之前的url没有?号,我们需要手动给他添加一个?号
if (!url.contains("?")) {
url = url.replaceFirst("&", "?");
}
//依据原来的request构造一个新的request,
Request request = oldRequest.newBuilder()
.url(url)
.build();
return chain.proceed(request);
}
} else {
if (paramMap != null && paramMap.size() > 0) {
RequestBody body = oldRequest.body();
if (body != null && body instanceof FormBody) {
FormBody formBody = (FormBody) body;
//1.把原来的的body里面的参数添加到新的body中
FormBody.Builder builder = new FormBody.Builder();
//为了防止重复添加相同的key和value
Map<String, String> temMap = new HashMap<>();
for (int i = 0; i < formBody.size(); i++) {
builder.add(formBody.encodedName(i), formBody.encodedValue(i));
temMap.put(formBody.encodedName(i), formBody.encodedValue(i));
}
//2.把公共请求参数添加到新的body中
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
if(!temMap.containsKey(entry.getKey())){
builder.add(entry.getKey(), entry.getValue());
}
}
FormBody newFormBody = builder.build();
//依据原来的request构造一个新的request,
Request newRequest = oldRequest.newBuilder()
.post(newFormBody)
.build();
return chain.proceed(newRequest);
}
}
}
return chain.proceed(oldRequest);
}
}
}
5.bean类
package com.example.thinkpad.wsjgowu;
import java.util.List;
public class CartInfo {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"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":1,"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":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":1,"pid":46,"price":234,"pscid":39,"selected":0,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":399,"createtime":"2017-10-14T21:38:26","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":82,"price":333,"pscid":85,"selected":0,"sellerid":3,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"},{"bargainPrice":1599,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/1993026402.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5863/302/8961270302/97126/41feade1/5981c81cNc1b1fbef.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7003/250/1488538438/195825/53bf31ba/5981c57eN51e95176.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5665/100/8954482513/43454/418611a9/5981c57eNd5fc97ba.jpg!q70.jpg","num":1,"pid":47,"price":111,"pscid":39,"selected":0,"sellerid":3,"subhead":"碳黑色 32GB 全网通 官方标配 1件","title":"锤子 坚果Pro 特别版 巧克力色 酒红色 全网通 移动联通电信4G手机 双卡双待 碳黑色 32GB 全网通"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":399,"createtime":"2017-10-14T21:38:26","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":83,"price":444,"pscid":85,"selected":0,"sellerid":4,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家4","sellerid":"4"},{"list":[{"bargainPrice":399,"createtime":"2017-10-03T23:53:28","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":84,"price":555,"pscid":85,"selected":0,"sellerid":5,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":399,"createtime":"2017-10-14T21:38:26","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":85,"price":666,"pscid":85,"selected":0,"sellerid":6,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"},{"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":62,"price":15999,"pscid":40,"selected":0,"sellerid":6,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家6","sellerid":"6"},{"list":[{"bargainPrice":399,"createtime":"2017-10-03T23:53:28","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":86,"price":777,"pscid":85,"selected":0,"sellerid":7,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"},{"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":1,"pid":30,"price":688,"pscid":2,"selected":0,"sellerid":7,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家7","sellerid":"7"},{"list":[{"bargainPrice":399,"createtime":"2017-10-03T23:53:28","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":87,"price":888,"pscid":85,"selected":0,"sellerid":8,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家8","sellerid":"8"},{"list":[{"bargainPrice":399,"createtime":"2017-10-14T21:38:26","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":2,"pid":88,"price":999,"pscid":85,"selected":0,"sellerid":9,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家9","sellerid":"9"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:43:53","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":33,"price":988,"pscid":2,"selected":0,"sellerid":10,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家10","sellerid":"10"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":10,"pid":56,"price":99,"pscid":39,"selected":0,"sellerid":12,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家12","sellerid":"12"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":4,"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":1,"pid":3,"price":198,"pscid":1,"selected":0,"sellerid":19,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"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":1,"pid":6,"price":7.99,"pscid":1,"selected":1,"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":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":1,"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;
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 : 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 : 1
* pid : 45
* price : 2999
* pscid : 39
* selected : 0
* sellerid : 1
* subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!
* title : 一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机
*/
private float bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private float price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
public float getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(float 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 float getPrice() {
return price;
}
public void setPrice(float 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;
}
}
}
}
6.自定义view的实现
package com.example.thinkpad.wsjgowu;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MyAddSubView extends LinearLayout{
//获取控件id
@BindView(R.id.sub_tv)
TextView subTv;
@BindView(R.id.product_number_tv)
TextView productNumberTv;
@BindView(R.id.add_tv)
TextView addTv;
private int number = 1;
public MyAddSubView(Context context) {
this(context,null);
}
public MyAddSubView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View view = inflate(context, R.layout.add_remove_view_layout, this);
//必须初始化
ButterKnife.bind(view);
}
@OnClick({R.id.sub_tv,R.id.add_tv})
public void onViewClicked(View view){
switch (view.getId()){
//点击减号
//在购买数量大于1的时候,每点击一次就减一,并付给数量值
case R.id.sub_tv:
if(number>1){
--number;
//给商品数量赋值
productNumberTv.setText(number+"");
if(onNumberChangeListener!=null){
onNumberChangeListener.onNumberChange(number);
}
}else{
Toast.makeText(getContext(),"不能在少了",Toast.LENGTH_LONG).show();
}
break;
//点击加号
case R.id.add_tv:
++number;
//给商品数量赋值
productNumberTv.setText(number+"");
if(onNumberChangeListener!=null){
//给接口传值
onNumberChangeListener.onNumberChange(number);
}
break;
}
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
OnNumberChangeListener onNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
this.onNumberChangeListener = onNumberChangeListener;
}
//通过接口告诉外面一共有几个商品
interface OnNumberChangeListener{
void onNumberChange(int num);
}
}
7,MyAdapter
package com.example.thinkpad.wsjgowu;
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 java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MyAdapter extends BaseExpandableListAdapter {
private List<CartInfo.DataBean> sellerData;
public MyAdapter(List<CartInfo.DataBean> sellerData){
this.sellerData=sellerData;
}
@Override
public int getGroupCount() {
return sellerData==null?0:sellerData.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return sellerData.get(groupPosition).getList()==null?0:sellerData.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//获取数据
CartInfo.DataBean dataBean = sellerData.get(groupPosition);
//创建商家类的viewhodel对象
ParentViewHolde parentViewHolde;
//判断当前方法中view为空的时候,将商家的布局添加进来
if(convertView==null){
//将商家的布局添加进来
convertView= View.inflate(parent.getContext(),R.layout.item_cart_parent,null);
//将填充慢的布局填充道商家的viewhodel中
parentViewHolde=new ParentViewHolde(convertView);
convertView.setTag(parentViewHolde);
}else{
//不为空就直接强转道 parentViewHolde中
parentViewHolde =(ParentViewHolde) convertView.getTag();
}
//为商家名字赋值
parentViewHolde.sellNameTv.setText(dataBean.getSellerName());
//根据商品名确定商家的checknox是否被选中
boolean currentSellerAllProductSelected = isCurrentSellerAllProductSelected(groupPosition);
//设置商家checkBox的状态
parentViewHolde.sellerCb.setChecked(currentSellerAllProductSelected);
//设置商家checkBox的点击事件
parentViewHolde.sellerCb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判断接口是否为空
if (onCartListChangeListener!=null){
//不为空就把商家序列号传入
onCartListChangeListener.onSellerCheckedChange(groupPosition);
}
}
});
return convertView;
}
//当前商家所有商品是否被选中
boolean isCurrentSellerAllProductSelected(int groupPosition) {
CartInfo.DataBean dataBean = sellerData.get(groupPosition);
//获取商品的集合
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
//循环商品集合
for(CartInfo.DataBean.ListBean listBean :list){
//只要一个没有被选中,商家就标记没选中
//listBean.getSelected() 获取商品的选中状态进行判断
if (listBean.getSelected() ==0){
return false;
}
}
return true;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//获取商品的集合
List<CartInfo.DataBean.ListBean> list = sellerData.get(groupPosition).getList();
//商品
CartInfo.DataBean.ListBean listBean = list.get(childPosition);
ChildViewHodel childViewHodel;
if(convertView ==null){
convertView=View.inflate(parent.getContext(),R.layout.item_cart_child,null);
childViewHodel = new ChildViewHodel(convertView);
convertView.setTag(childViewHodel);
}else{
childViewHodel = (ChildViewHodel) convertView.getTag();
}
//商品名字
childViewHodel.productTitleNameTv.setText(listBean.getTitle());
//商品单价
childViewHodel.productPriceTv.setText(listBean.getPscid()+"");
//商品的checkBox状态
childViewHodel.childCb.setChecked(listBean.getSelected() ==1);
//商品的checkBox的点击事件
childViewHodel.childCb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onCartListChangeListener!=null){
onCartListChangeListener.onProductCheckedChange(groupPosition,childPosition);
}
}
});
childViewHodel.addRemoveView.setNumber(listBean.getNum());
childViewHodel.addRemoveView.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
@Override
public void onNumberChange(int num) {
//拿到上坪最新的数量
if(onCartListChangeListener!=null){
onCartListChangeListener.onProductNumberChange(groupPosition,childPosition,num);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
//所有商品是否被选中
public boolean isAllProductsSelected() {
for (int i = 0; i < sellerData.size(); i++) {
CartInfo.DataBean dataBean = sellerData.get(i);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 0) {
return false;
}
}
}
return true;
}
//计算总价
public float calculateTotalPrice() {
float totalPrice = 0;
for (int i = 0; i < sellerData.size(); i++) {
CartInfo.DataBean dataBean = sellerData.get(i);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
//只要是选中状态
if (list.get(j).getSelected() == 1) {
float price = list.get(j).getPrice();
int num = list.get(j).getNum();
totalPrice += price * num;
}
}
}
return totalPrice;
}
public int calculateTotalNumber() {
int totalNumber = 0;
for (int i = 0; i < sellerData.size(); i++) {
CartInfo.DataBean dataBean = sellerData.get(i);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
//只要是选中状态
if (list.get(j).getSelected() == 1) {
int num = list.get(j).getNum();
totalNumber += num;
}
}
}
return totalNumber;
}
//当商家得checkbox被点击得时候调用,设置当前商家得所有商品得状态
public void changeCurrentSellerAllProductsStatus(int groupPosition, boolean isSelected) {
CartInfo.DataBean dataBean = sellerData.get(groupPosition);
List<CartInfo.DataBean.ListBean> listBeans = dataBean.getList();
for (int i = 0; i < listBeans.size(); i++) {
CartInfo.DataBean.ListBean listBean = listBeans.get(i);
listBean.setSelected(isSelected ? 1 : 0);
}
}
//当商品得checkbox被点击得时候调用,改变当前商品状态
public void changeCurrentProductStatus(int groupPosition, int childPosition) {
CartInfo.DataBean dataBean = sellerData.get(groupPosition);
List<CartInfo.DataBean.ListBean> listBeans = dataBean.getList();
CartInfo.DataBean.ListBean listBean = listBeans.get(childPosition);
listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
}
//当加减器被点击得时候调用,改变当前商品得数量
public void changeCurrentProductNumber(int groupPosition, int childPosition, int number) {
CartInfo.DataBean dataBean = sellerData.get(groupPosition);
List<CartInfo.DataBean.ListBean> listBeans = dataBean.getList();
CartInfo.DataBean.ListBean listBean = listBeans.get(childPosition);
listBean.setNum(number);
}
//设置所有商品得状态
public void changeAllProductsStatus(boolean selected) {
for (int i = 0; i < sellerData.size(); i++) {
CartInfo.DataBean dataBean = sellerData.get(i);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setSelected(selected?1:0);
}
}
}
//商家数据的viewhodel
class ParentViewHolde{
@BindView(R.id.seller_cb)
CheckBox sellerCb;
@BindView(R.id.seller_name_tv)
TextView sellNameTv;
ParentViewHolde(View view){
ButterKnife.bind(this,view);
}
}
class ChildViewHodel{
@BindView(R.id.child_cb)
CheckBox childCb;
@BindView(R.id.product_icon_iv)
ImageView productIconIv;
@BindView(R.id.product_title_name_tv)
TextView productTitleNameTv;
@BindView(R.id.product_price_tv)
TextView productPriceTv;
@BindView(R.id.add_remove_view)
MyAddSubView addRemoveView;
ChildViewHodel(View view){
//进行绑定
ButterKnife.bind(this,view);
}
}
onCartListChangeListener onCartListChangeListener;
public void setOnCartListChangeListener(MyAdapter.onCartListChangeListener onCartListChangeListener) {
this.onCartListChangeListener = onCartListChangeListener;
}
//定义一个接口用来传递商家和商品 还有增减器的变化
public interface onCartListChangeListener{
//传递的商家
void onSellerCheckedChange(int groupPostition);
//传递精确的商品
void onProductCheckedChange(int groupPosition, int childPosition);
//精确商品的增减器的变化
void onProductNumberChange(int groupPosition, int childPosition, int number);
}
}
8.MainActivity
package com.example.thinkpad.wsjgowu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.el_cart)
ExpandableListView elCart;
@BindView(R.id.cb_care_all_select)
CheckBox cbCartAllSelect;
@BindView(R.id.tv_cart_total_price)
TextView tvCartTotalPrice;
@BindView(R.id.btn_cart_pay)
Button btnCartPay;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initData();
}
private void initData() {
String url="https://www.zhaoapi.cn/product/getCarts";
HashMap<String,String> map = new HashMap<>();
map.put("uid","71");
OkhttpUtils.getInstance().doPost(url, map, new OkhttpUtils.OkCallback() {
@Override
public void onFailure(Exception e) {
}
@Override
public void onResponse(String json) {
Gson gson = new Gson();
CartInfo cartInfo = gson.fromJson(json, CartInfo.class);
if("0".equalsIgnoreCase(cartInfo.getCode())){
List<CartInfo.DataBean> sellerData = cartInfo.getData();
//设置adaper
myAdapter = new MyAdapter(sellerData);
myAdapter.setOnCartListChangeListener(new MyAdapter.onCartListChangeListener() {
@Override
public void onSellerCheckedChange(int groupPosition) {
//商家被点击
boolean currentSellerAllProductSelected = myAdapter.isCurrentSellerAllProductSelected(groupPosition);
myAdapter.changeCurrentSellerAllProductsStatus(groupPosition, !currentSellerAllProductSelected);
myAdapter.notifyDataSetChanged();
//刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void onProductCheckedChange(int groupPosition, int childPosition) {
//点击商品得checkbox
myAdapter.changeCurrentProductStatus(groupPosition,childPosition);
myAdapter.notifyDataSetChanged();
//刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void onProductNumberChange(int groupPosition, int childPosition, int number) {
//当加减被点击
myAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
myAdapter.notifyDataSetChanged();
//刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
//联网更新网络上得商品数量
}
});
elCart.setAdapter(myAdapter);
//展开二级列表
for (int i=0;i<sellerData.size();i++){
elCart.expandGroup(i);
}
//刷新checkbox状态和总价和总数量
refreshSelectedAndTotalPriceAndTotalNumber();
}
}
});
}
//刷新checkbox状态和总价和总数量
private void refreshSelectedAndTotalPriceAndTotalNumber() {
//去判断是否所有得商品都被选中
boolean allProductsSelected = myAdapter.isAllProductsSelected();
//设置给全选checkBox
cbCartAllSelect.setChecked(allProductsSelected);//cbCartAllSelect
//计算总价
float totalPrice = myAdapter.calculateTotalPrice();
tvCartTotalPrice.setText("总价 " + totalPrice);
//计算总数量
int totalNumber = myAdapter.calculateTotalNumber();
btnCartPay.setText("去结算(" + totalNumber + ")");
}
@OnClick(R.id.cb_care_all_select)
public void onViewClicked() {
//底部全选按钮
//时候所有得商品都被选中
boolean allProductsSelected = myAdapter.isAllProductsSelected();
myAdapter.changeAllProductsStatus(!allProductsSelected);
myAdapter.notifyDataSetChanged();
//刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
}