//main布局
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ExpandableListView
android:id="@+id/elc_show_main"
android:layout_weight="1"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="0dp">
</ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_allCheck_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:textColor="#000" />
<TextView
android:id="@+id/btn_allPrice_main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="价格"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp" />
<Button
android:id="@+id/btn_allNumber_main"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="去结算(0)" />
</LinearLayout>
</LinearLayout>
//main页面代码
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
String url = "https://www.zhaoapi.cn/product/getCarts?uid=71";
private ExpandableListView elc_show_main;
private CheckBox cb_allCheck_main;
private TextView btn_allPrice_main;
private Button btn_allNumber_main;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
//得到okhttp
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(final Call call, Response response) throws IOException {
final String string = response.body().string();
runOnUiThread(new Runnable() {
private List<CartInfo.DataBean> sellerData;
@Override
public void run() {
Gson gson = new Gson();
CartInfo cartInfo = gson.fromJson(string, CartInfo.class);
sellerData = cartInfo.getData();
//创建适配
//创建适配
myAdapter = new MyAdapter(sellerData,MainActivity.this);
//调用适配器的借口 来实现回电数据
myAdapter.setOnCartListChangeListener(new MyAdapter.OnCartListChangeListener() {
@Override
public void SellerSelectedChange(int groupPosition) {
//先得到 checkbox
boolean b = myAdapter.isCurrentSellerAllProductSelected(groupPosition);
//改变所有当前商家的选中状态
myAdapter.changeCurrentSellerAllProductSelected(groupPosition,!b);
myAdapter.notifyDataSetChanged();
refreshAllSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void changeCurrentProductSelected(int groupPosition, int childPosition) {
myAdapter.changeCurrentProductSelected(groupPosition,childPosition);
myAdapter.notifyDataSetChanged();
refreshAllSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void ProductNumberChange(int groupPosition, int childPosition, int number) {
myAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
myAdapter.notifyDataSetChanged();
//刷新底部的方法
refreshAllSelectedAndTotalPriceAndTotalNumber();
}
});
elc_show_main.setAdapter(myAdapter);
//
for (int i = 0; i < sellerData.size() ; i++) {
elc_show_main.expandGroup(i);
}
}
});
}
});
}
private void initView() {
elc_show_main = (ExpandableListView) findViewById(R.id.elc_show_main);
cb_allCheck_main = (CheckBox) findViewById(R.id.cb_allCheck_main);
btn_allPrice_main = (TextView) findViewById(R.id.btn_allPrice_main);
btn_allNumber_main = (Button) findViewById(R.id.btn_allNumber_main);
cb_allCheck_main.setOnClickListener(this);
btn_allNumber_main.setOnClickListener(this);
}
private void refreshAllSelectedAndTotalPriceAndTotalNumber(){
boolean allProductsSelected = myAdapter.isAllProductsSelected();
cb_allCheck_main.setChecked(allProductsSelected);
//计算总金额
Double totalPrice = myAdapter.calculateTotalPrice();
btn_allPrice_main.setText("总价:¥"+totalPrice);
//计算总数量
int totalNumber = myAdapter.calculateTotalNumber();
btn_allNumber_main.setText("去结算("+totalNumber+")");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_allNumber_main:
break;
case R.id.cb_allCheck_main:
boolean allProductsSelected = myAdapter.isAllProductsSelected();
myAdapter.changeAllProductsSelected(!allProductsSelected);
myAdapter.notifyDataSetChanged();
//刷新底部的方法
refreshAllSelectedAndTotalPriceAndTotalNumber();
break;
}
}
}
//bean封装
package com.bwie.xiaqin.gouwuche_erjiliebiao;
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":4,"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":11800,"createtime":"2017-10-14T21:48:08","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":65,"price":12000,"pscid":40,"selected":0,"sellerid":9,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家9","sellerid":"9"},{"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":1,"pid":66,"price":13000,"pscid":40,"selected":0,"sellerid":10,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家10","sellerid":"10"},{"list":[{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":100,"price":2200,"pscid":112,"selected":0,"sellerid":11,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"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":4,"pid":34,"price":9,"pscid":2,"selected":0,"sellerid":11,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家11","sellerid":"11"},{"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":71,"price":32999,"pscid":40,"selected":0,"sellerid":15,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家15","sellerid":"15"},{"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":1,"pid":72,"price":33999,"pscid":40,"selected":0,"sellerid":16,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:38:26","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":23,"price":188,"pscid":1,"selected":0,"sellerid":16,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家16","sellerid":"16"},{"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":40,"price":345,"pscid":2,"selected":0,"sellerid":17,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"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":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":41,"price":456,"pscid":2,"selected":0,"sellerid":18,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"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":3,"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":4,"pid":3,"price":198,"pscid":1,"selected":0,"sellerid":19,"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":9,"pid":75,"price":36999,"pscid":40,"selected":0,"sellerid":19,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家19","sellerid":"19"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":2,"pid":76,"price":37999.99,"pscid":40,"selected":1,"sellerid":20,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家20","sellerid":"20"},{"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":36,"pid":5,"price":88.99,"pscid":1,"selected":1,"sellerid":21,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家21","sellerid":"21"},{"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":23,"pid":6,"price":7.99,"pscid":1,"selected":0,"sellerid":22,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家22","sellerid":"22"}]
*/
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* list : [{"bargainPrice":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.0
* 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.0
* pscid : 39
* selected : 0
* sellerid : 1
* subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!
* title : 一加手机5 (A5000) 6GB+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;
}
}
}
}
//MyAddSubView
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;
/**
* Created by lenovo on 2018/9/24.
*/
public class MyAddSubView extends LinearLayout implements View.OnClickListener {
TextView tv_sub_view;
TextView tv_number_view;
TextView tv_add_view;
private int number = 1;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
tv_number_view.setText(number + "");
}
public MyAddSubView(Context context){
super(context,null);
}
public MyAddSubView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View inflate = View.inflate(context, R.layout.add_sub_view, this);
tv_sub_view = inflate.findViewById(R.id.tv_sub_view);
tv_number_view = inflate.findViewById(R.id.tv_number_view);
tv_add_view = inflate.findViewById(R.id.tv_add_view);
tv_add_view.setOnClickListener(this);
tv_sub_view.setOnClickListener(this);
}
OnNumberChangeListener onNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
this.onNumberChangeListener = onNumberChangeListener;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_sub_view:
if (number>1){
--number;
tv_number_view.setText(number+"");
if (onNumberChangeListener != null){
onNumberChangeListener.OnNumberChange(number);
}
}else {
Toast.makeText(getContext(), "不能再少啦!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.tv_add_view:
++number;
tv_number_view.setText(number+"");
if (onNumberChangeListener!=null){
onNumberChangeListener.OnNumberChange(number);
}
break;
}
}
public interface OnNumberChangeListener {
void OnNumberChange(int number);
}
}
//add_sub_view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="#000"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:id="@+id/tv_sub_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="-"
android:textColor="#000" />
<TextView
android:id="@+id/tv_number_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="1"
android:textColor="#000" />
<TextView
android:id="@+id/tv_add_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="+"
android:textColor="#000" />
</LinearLayout>
//MyAdapter
public class MyAdapter extends BaseExpandableListAdapter{
List<CartInfo.DataBean> sellerData;
Context context;
public MyAdapter(List<CartInfo.DataBean> sellerData, Context context) {
this.sellerData = sellerData;
this.context = context;
}
@Override
public int getGroupCount() {
return sellerData.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 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) {
GroupViewHolder groupViewHolder = null;
if (convertView == null) {
groupViewHolder = new GroupViewHolder();
convertView = View.inflate(context, R.layout.group_view_layout_shopcar, null);
groupViewHolder.cb_group_item = convertView.findViewById(R.id.cb_group_item);
groupViewHolder.tv_title_group = convertView.findViewById(R.id.tv_title_group);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
//赋值
CartInfo.DataBean dataBean = sellerData.get(groupPosition);
//商家的名称
groupViewHolder.tv_title_group.setText(dataBean.getSellerName());
boolean b = isCurrentSellerAllProductSelected(groupPosition);
//商家的checkbox
groupViewHolder.cb_group_item.setChecked(b);
//点击checkbox触发监听
groupViewHolder.cb_group_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onCartListChangeListener != null) {
onCartListChangeListener.SellerSelectedChange(groupPosition);
}
}
});
return convertView;
}
public void changeCurrentSellerAllProductSelected(int Position, boolean b) {
List<CartInfo.DataBean.ListBean> list = sellerData.get(Position).getList();
for (int i = 0; i < list.size(); i++) {
list.get(i).setSelected(b ? 1 : 0);
}
}
//用来判断外层的Selected值 如果是0 不选中 否则选中
public boolean isCurrentSellerAllProductSelected(int position) {
List<CartInfo.DataBean.ListBean> list = sellerData.get(position).getList();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getSelected() == 0) {
return false;
}
}
return true;
}
//设置子层
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder = null;
if (convertView == null) {
childViewHolder = new ChildViewHolder();
convertView = View.inflate(context, R.layout.child_view_layout_shopcar, null);
childViewHolder.cb_child_item = convertView.findViewById(R.id.cb_child_item);
childViewHolder.tv_title_child = convertView.findViewById(R.id.tv_title_child);
childViewHolder.tv_price_child = convertView.findViewById(R.id.tv_price_child);
childViewHolder.iv_icon_child = convertView.findViewById(R.id.iv_icon_child);
childViewHolder.add_sub_view_child = convertView.findViewById(R.id.add_sub_view_child);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
//赋值
CartInfo.DataBean.ListBean listBean = sellerData.get(groupPosition).getList().get(childPosition);
//商品名称
childViewHolder.tv_title_child.setText(listBean.getTitle());
//商品图片
String images = listBean.getImages();
String[] split = images.split("\\|");
Picasso.get().load(split[0]).into(childViewHolder.iv_icon_child);
//商品价格
childViewHolder.tv_price_child.setText("¥" + listBean.getPrice());
//商品checkb0x
childViewHolder.cb_child_item.setChecked(listBean.getSelected() == 1);
childViewHolder.cb_child_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onCartListChangeListener.changeCurrentProductSelected(groupPosition, childPosition);
}
});
childViewHolder.add_sub_view_child.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
@Override
public void OnNumberChange(int number) {
onCartListChangeListener.ProductNumberChange(groupPosition, childPosition, number);
}
});
return convertView;
}
public void changeCurrentProductNumber(int groupPosition, int childPosition, int number) {
CartInfo.DataBean.ListBean listBean = sellerData.get(groupPosition).getList().get(childPosition);
listBean.setNum(number);
}
public void changeCurrentProductSelected(int groupPosition, int childPosition) {
CartInfo.DataBean.ListBean listBean = sellerData.get(groupPosition).getList().get(childPosition);
listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
}
public double calculateTotalPrice() {
double totalPrice = 0;
for (int i = 0; i < sellerData.size(); i++) {
List<CartInfo.DataBean.ListBean> list = sellerData.get(i).getList();
for (int j = 0; j < list.size(); j++) {
//只有选中采取计算
if (list.get(j).getSelected() == 1) {
double 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++) {
List<CartInfo.DataBean.ListBean> list = sellerData.get(i).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;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public boolean isAllProductsSelected() {
for (int i = 0; i < sellerData.size(); i++) {
List<CartInfo.DataBean.ListBean> list = sellerData.get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 0) {
return false;
}
}
}
return true;
}
public void changeAllProductsSelected(boolean b) {
for (int i = 0; i < sellerData.size(); i++) {
List<CartInfo.DataBean.ListBean> list = sellerData.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setSelected(b ? 1 : 0);
}
}
}
public static class GroupViewHolder {
public CheckBox cb_group_item;
public TextView tv_title_group;
}
public static class ChildViewHolder {
public CheckBox cb_child_item;
public ImageView iv_icon_child;
public TextView tv_title_child;
public TextView tv_price_child;
public MyAddSubView add_sub_view_child;
}
OnCartListChangeListener onCartListChangeListener;
public void setOnCartListChangeListener(OnCartListChangeListener onCartListChangeListener) {
this.onCartListChangeListener = onCartListChangeListener;
}
public interface OnCartListChangeListener {
void SellerSelectedChange(int groupPosition);
void changeCurrentProductSelected(int groupPosition, int childPosition);
void ProductNumberChange(int groupPosition, int childPosition, int number);
}
}
//group_view_layout_shopcar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_group_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_title_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ellipsize="end"
android:maxLines="2"
android:text="标题"
android:textColor="#000"
android:textSize="20sp" />
</LinearLayout>
//child_view_layout_shopcar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_child_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<ImageView
android:id="@+id/iv_icon_child"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="标题"
android:textColor="#000" />
<TextView
android:id="@+id/tv_price_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp" />
</LinearLayout>
<com.bwie.xiaqin.gouwuche_erjiliebiao.MyAddSubView
android:id="@+id/add_sub_view_child"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp">
</com.bwie.xiaqin.gouwuche_erjiliebiao.MyAddSubView>
</LinearLayout>
//依赖
//Gson
implementation 'com.google.code.gson:gson:2.2.4'
//OkHttp
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.github.bumptech.glide:glide:4.8.0'