一,导入所需依赖,添加网络权限
compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile "io.reactivex.rxjava2:rxjava:2.1.7" compile 'com.facebook.fresco:fresco:1.5.0' compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.android.support:recyclerview-v7:26.1.0+'清单文件中
android:name="android.permission.INTERNET"/>
二,项目结构
布局文件activity_main
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.peng.goodscard_1510d.view.MainActivity">
android:layout_height="40dp"
android:background="#ff3660"
android:gravity="center"
android:text="商品详情"
android:textColor="#ffffff"
android:textSize="25sp" />
android:layout_width="match_parent"
android:layout_height="400dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="match_parent"
android:layout_weight="1">
android:layout_height="40dp">
android:layout_width="0dp"
android:background="#33000000"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="购物车" />
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#33000000"
android:gravity="center"
android:text="加入购物车" />
activity_second
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bwie.com.a1510dproject.view.GoodsCardActivity">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
android:layout_height="40dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="全选" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/cb"
android:text="合计:" />
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#ff0000"
android:gravity="center"
android:text="去结算(0)"
android:textColor="#ffffff" />
elv_child
android:layout_height="120dp"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="50dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="100dp"
android:layout_height="100dp" />
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/ivDel"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/iv_del" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:text="1"/>
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/iv_add" />
elv_group
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical" />
三,创建Api接口类和创建接口定义一个请求数据的方法
public class Api { //https://www.zhaoapi.cn/product/getCarts?uid=71&source=android public final static String str1 = "https://www.zhaoapi.cn/"; public final static String str2 = "product/getCarts?uid=71&source=android"; }
public interface ApiServise { @GET(Api.str2) Observable四,请求网络数据,创建好bean类getData(); }
public class MyBean { /** * code : 0 * data : [{"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":91,"price":1344,"pscid":112,"selected":1,"sellerid":2,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"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手机"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-03T23:43:53","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":12,"price":256,"pscid":1,"selected":0,"sellerid":5,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家5","sellerid":"5"},{"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":63,"price":10000,"pscid":40,"selected":1,"sellerid":7,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家7","sellerid":"7"},{"list":[{"bargainPrice":2999,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2385655.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t2068/298/2448145915/157953/7be197df/56d51a42Nd86f1c8e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2437/128/1687178395/117431/bcc190c1/56d3fcbaNb2963d21.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2467/222/2263160610/95597/927b8a2f/56d3eafeNdecebeb6.jpg!q70.jpg","num":1,"pid":53,"price":777,"pscid":39,"selected":1,"sellerid":9,"subhead":"Super AMOLED三星双曲面2K 屏,支持无线充电!","title":"三星 Galaxy S7 edge(G9350)4GB+32GB 铂光金 移动联通电信4G手机 双卡双待"}],"sellerName":"商家9","sellerid":"9"},{"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":1,"pid":55,"price":5999,"pscid":39,"selected":1,"sellerid":11,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家11","sellerid":"11"},{"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":3,"pid":56,"price":99,"pscid":39,"selected":1,"sellerid":12,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家12","sellerid":"12"},{"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":1,"pid":39,"price":234,"pscid":2,"selected":1,"sellerid":16,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家16","sellerid":"16"},{"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":77,"price":38999.99,"pscid":40,"selected":1,"sellerid":21,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家21","sellerid":"21"}] * msg : 请求成功 */ private String code; private String msg; private List五,model层data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List getData() { return data; } public void setData(List data) { this.data = data; } public static class DataBean { /** * 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":91,"price":1344,"pscid":112,"selected":1,"sellerid":2,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"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手机"}] * sellerName : 商家2 * sellerid : 2 */ private String sellerName; private String sellerid; private List list; private boolean isChecked; public boolean isChecked() { return isChecked; } public void setChecked(boolean checked) { isChecked = checked; } 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 getList() { return list; } public void setList(List list) { this.list = list; } public static class ListBean { /** * bargainPrice : 159.0 * 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 : 91 * price : 1344.0 * pscid : 112 * selected : 1 * sellerid : 2 * subhead : 针织针织闪闪闪亮你的眼 * title : 维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL */ 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; private boolean checked; private int count = 1; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } 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; } } } }
1,model接口
public interface IModel { void getDatas(Iview iview); }2,model类
public class ClassModel implements IModel{ @Override public void getDatas(final Iview iview) { //在此方法中初始化Retrofit Retrofit retrofit = new Retrofit.Builder() .baseUrl(Api.str1) //调用rajava工厂类 .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //调用retrofit自己的Gosn解析方法 .addConverterFactory(GsonConverterFactory.create()) .build(); // 通过动态代理获得网络接口的数据 ApiServise apiServise = retrofit.create(ApiServise.class); //调用被观察者的方法 Observable六,view层data = apiServise.getData(); 设置请求数据时候的线程 data.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer () { @Override public void onCompleted() { } @Override public void onError(Throwable e) { //请求数据失败的方法 Log.i("onError",e.getMessage()); } @Override public void onNext(MyBean myBean) { iview.success(myBean); } }); } }
public interface Iview { void success(MyBean myBean); void error(Exception e); }七,presenter层
public class MyPresenter { Iview iview; IModel iModel; public MyPresenter(Iview iview) { this.iview = iview; this.iModel = new ClassModel(); } //创建获取数据的方法 public void getDatas(){ iModel.getDatas(new Iview() { @Override public void success(MyBean myBean) { iview.success(myBean); } @Override public void error(Exception e) { } }); } }八,适配器
public class MyAdapter extends BaseExpandableListAdapter{ //上下文和数据源 private Context context; private Listgroup; private List > child; //private final LayoutInflater inflater; public MyAdapter(Context context, List
group, List > child) { this.context = context; this.group = group; this.child = child; } @Override public int getGroupCount() { return group.size(); } @Override public int getChildrenCount(int groupPosition) { return child.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return group.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return child.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 false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final GroupViewHolder groupViewHolder; if (convertView==null){ //创建一级布局 View view = View.inflate(context,R.layout.item_group,null); convertView=view; groupViewHolder = new GroupViewHolder(); //初始化控件 groupViewHolder.tv = view.findViewById(R.id.tvGroup); groupViewHolder.cbGroup = view.findViewById(R.id.cbGroup); //放到容器中 view.setTag(groupViewHolder); }else { //从容器中取出来 groupViewHolder = (GroupViewHolder) convertView.getTag(); } //赋值 final MyBean.DataBean dataBean = group.get(groupPosition); groupViewHolder.tv.setText(dataBean.getSellerName()); groupViewHolder.cbGroup.setChecked(dataBean.isChecked()); //商家的多选框的点击事件 groupViewHolder.cbGroup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //需要改变三个checkbox的状态值 //1.一级列表的checkbox状态值,需要在bean中添加变量并给get,set方法 dataBean.setChecked(groupViewHolder.cbGroup.isChecked()); //2.二级列表的checkbox状态值 setChildrenCb(groupPosition, groupViewHolder.cbGroup.isChecked()); //3.全选的checkbox状态值,相当于调用了Activity中的全选的方法 ((MainActivity) context).setAllChecked(isAllGroupCbChecked()); //计算钱和数量并显示 setPriceAndCount(); //刷新界面 notifyDataSetChanged(); } }); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ChildViewHolder holder; if (convertView==null){ View view = View.inflate(context,R.layout.item_child,null); convertView=view; holder = new ChildViewHolder(); holder.iv = view.findViewById(R.id.iv); holder.tvTitle = view.findViewById(R.id.tvTitle); holder.tvSubhead = view.findViewById(R.id.tvSubhead); holder.tvPrice = view.findViewById(R.id.tvPrice); holder.cbChild = view.findViewById(R.id.cbChild); holder.btDel = view.findViewById(R.id.btDel); holder.tvNum = view.findViewById(R.id.tvNum); holder.ivDel = view.findViewById(R.id.ivDel); holder.ivAdd = view.findViewById(R.id.ivAdd); view.setTag(holder); }else { holder = (ChildViewHolder) convertView.getTag(); } //赋值,首先获得二级列表的集合 final MyBean.DataBean.ListBean listBean = child.get(groupPosition).get(childPosition); //图片路径进行拆分赋值 String images = listBean.getImages(); Glide.with(context).load(images.split("\\|")[0]).into(holder.iv); holder.tvTitle.setText(listBean.getTitle()); holder.cbChild.setChecked(child.get(groupPosition).get(childPosition).isChecked()); holder.tvSubhead.setText(listBean.getSubhead()); holder.tvPrice.setText(listBean.getPrice() + "元"); holder.tvNum.setText(listBean.getCount() + ""); //给二级checkbox设置点击事件 holder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //需要改变三个checkbox的状态值 //1.二级列表的checkbox状态值 listBean.setChecked(holder.cbChild.isChecked()); //2.一级列表的checkbox状态值 group.get(groupPosition).setChecked(isAllChildCbChecked(groupPosition)); //3.全选的checkbox状态值 ((MainActivity) context).setAllChecked(isAllGroupCbChecked()); //计算钱和数量并显示 setPriceAndCount(); //刷新界面 notifyDataSetChanged(); } }); //加号的点击监听 holder.ivAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取目前显示的值 int count = listBean.getCount(); count++; //改变JavaBean里的状态值 listBean.setCount(count); //计算钱和数量并显示 setPriceAndCount(); //刷新列表 notifyDataSetChanged(); } }); //减号的点击监听 holder.ivDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取目前显示的值 int count = listBean.getCount(); if (count <= 1) { count = 1; } else { count--; } //改变JavaBean里的状态值 listBean.setCount(count); //计算钱和数量并显示 setPriceAndCount(); //刷新列表 notifyDataSetChanged(); } }); //删除按钮的监听 holder.btDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //其实就是删除集合 List
listBeans = child.get(groupPosition); if (listBeans.size() > 0) { listBeans.remove(childPosition); } if (listBeans.size() == 0) { child.remove(groupPosition); group.remove(groupPosition); } //计算钱和数量并显示 setPriceAndCount(); //改变全选状态 ((MainActivity) context).setAllChecked(isAllGroupCbChecked()); //刷新列表 notifyDataSetChanged(); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class GroupViewHolder{ TextView tv; CheckBox cbGroup; } class ChildViewHolder{ ImageView iv; TextView tvTitle; TextView tvSubhead; TextView tvPrice; CheckBox cbChild; Button btDel; TextView tvNum; ImageView ivDel; ImageView ivAdd; } //设置一级列表和二级列表多选框的设置状态 private void setChildrenCb(int groupPosition, boolean bool) { List listBeans = child.get(groupPosition); for (int i = 0; i < listBeans.size(); i++) { listBeans.get(i).setChecked(bool); } } //判断一级列表checkbox状态 private boolean isAllGroupCbChecked() { if (group.size() == 0) { return false; } for (int i = 0; i < group.size(); i++) { if (!group.get(i).isChecked()) { return false; } } return true; } //判断二级列表checkbox状态 private boolean isAllChildCbChecked(int groupPosition) { List listBeans = child.get(groupPosition); for (int i = 0; i < listBeans.size(); i++) { if (!listBeans.get(i).isChecked()) { return false; } } return true; } //设钱和数量 private void setPriceAndCount() { ((MainActivity) context).setPriceAndCount(compute()); } //计算钱和数量 private PriceAndCount compute() { double price = 0; int count = 0; for (int i = 0; i < group.size(); i++) { List listBeans = child.get(i); for (int j = 0; j < listBeans.size(); j++) { if (listBeans.get(j).isChecked()) { price += listBeans.get(j).getPrice() * listBeans.get(j).getCount(); count += listBeans.get(j).getCount(); } } } return new PriceAndCount(price, count); } //全选或者全不选的方法 public void AllOrNone(boolean bool) { for (int i = 0; i < group.size(); i++) { group.get(i).setChecked(bool); setChildrenCb(i, bool); } setPriceAndCount(); notifyDataSetChanged(); } }
九,注意事项,在适配器中,因为要用到商家和商品的多选框的状态,需要在bean里对应的集合中添加是否选中的属性
1,在商家的一级集合中添加,并给set,get方法
private boolean checked;2,二级商品集合中,因为要用到商品的数量进行加减操作,所以需要再添加一个int类型的count属性,同样给set,get方法
private boolean checked; private int count = 1;3,需要写一个新的bean类,用来放计算总数和总价的属性
public class PriceAndCount { private double price; private int count; public PriceAndCount(double price, int count) { this.price = price; this.count = count; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
十,activity
public class MainActivity extends AppCompatActivity implements Iview{ private Context context; private List> child = new ArrayList<>(); @BindView(R.id.elv) ExpandableListView elv; @BindView(R.id.cb) CheckBox cb; @BindView(R.id.tvTotal) TextView tvTotal; @BindView(R.id.tvCount) TextView tvCount; private MyPresenter myPresenter; private MyAdapter myAdapter; private LinearLayoutManager layoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //实例化P myPresenter = new MyPresenter(this); myPresenter.getDatas(); //全选监听 cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //调用适配器中的方法 myAdapter.AllOrNone(cb.isChecked()); } }); } @Override public void success(MyBean myBean) { //获取数据成功以后得到一级集合的数据 List
group = myBean.getData(); //通过循环取出二级集合的数据 for (int i=0;i ;i++){ List list = group.get(i).getList(); child.add(list); } //初始适配器 myAdapter = new MyAdapter(MainActivity.this,group,child); elv.setAdapter(myAdapter); //默认展开列表,不写的话无法展开
for (int i=0;i;i++){ elv.expandGroup(i); } } @Override public void error(Exception e) { } //是否全选 public void setAllChecked(boolean bool) { cb.setChecked(bool); } //计算总价和总数的方法 public void setPriceAndCount(PriceAndCount priceAndCount) { tvCount.setText("合计:" + priceAndCount.getPrice()); tvTotal.setText("去结算(" + priceAndCount.getCount() + ")"); }}