服务器之家

服务器之家 > 正文

RxJava+Retrofit+Mvp实现购物车

时间:2021-04-28 11:34     来源/作者:Cyq_0927

本文实例为大家分享了rxjava retrofit实现购物车展示的具体代码,供大家参考,具体内容如下

先给大家展示一下效果图

RxJava+Retrofit+Mvp实现购物车

框架结构:

1.项目框架:mvp,图片加载用fresco,网络请求用okhttp+retrofit实现(自己封装,加单例模式),
2.完成购物车数据添加(如果接口无数据,可用接口工具添加数据),
3.自定义view实现加减按钮,每次点击加减,item中的总数及总价要做出相应的改变。
4.当数量为1时,点击减号,数量不变,吐司提示用户最小数量为1。
5.底部总数及总价为所有item项中的总价及总数,每个item中数量价格的更改,底部总价总数要与之联动
6.实现单选反选全选功能,首次进入默认全选,item未选中时总数及总价不计入底部数据,改变选中状态时,底部总数及总价能做出正确修改
7.点击删除按钮,删除item,底部总数及总价能做出正确修改,接口数据删除。

添加所需要的依赖和联网权限

?
1
2
3
4
5
6
7
8
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//retrofit2所需要的包
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//converterfactory的gson依赖包
compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//converterfactory的string依赖包
compile 'io.reactivex.rxjava2:rxjava:2.1.3'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'de.greenrobot:eventbus:3.0.0-beta1'
compile 'com.facebook.fresco:fresco:0.11.0'
?
1
<uses-permission android:name="android.permission.internet" />

在使用mvp模式前先分包

net包

api

?
1
2
3
public class api {
 public static string host="http://120.27.23.105/product/";
}

serviceapi

?
1
2
3
4
5
6
7
8
public interface serviceapi {
 @get("getproductdetail")
 flowable<xqbean> getxq(@query("pid")int pid, @query("source")string str);
 @get("addcart")
 flowable<addcart> getmsg(@query("uid")int uid, @query("pid")int pid, @query("source")string str);
 @get("getcarts")
 flowable<cartbean> getcart(@query("uid")int uid, @query("source")string str);
}

成功的接口

?
1
2
3
public interface onnetlisenter<t> {
 void success(t t);
}

retrofit的封装

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class retrofithepler {
 public static okhttpclient okhttpclient;
 public static serviceapi serviceapi;
 /**
 * 优先执行
 */
 static {
 getokhttpclient();
 }
 public static okhttpclient getokhttpclient(){
 if(okhttpclient==null){
 synchronized (okhttpclient.class){
 if(okhttpclient==null){
  okhttpclient=new okhttpclient();
 }
 }
 }
 return okhttpclient;
 }
 public static serviceapi getserviceapi(){
 
 if(serviceapi==null){
 synchronized (okhttpclient.class){
 if(serviceapi==null){
  serviceapi=oncreate(serviceapi.class,api.host);
 }
 }
 }
 return serviceapi;
 }
 public static <t> t oncreate(class<t>tclass,string url){
 retrofit retrofit = new retrofit.builder()
 .baseurl(url)
 .addconverterfactory(gsonconverterfactory.create())
 .client(okhttpclient)
 .addcalladapterfactory(rxjava2calladapterfactory.create())
 //.addcalladapterfactory(rxjava2calladapterfactory.create())
 .build();
 return retrofit.create(tclass);
 }
}

bean包

详情页的bean类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
public class xqbean {
 /**
 * msg :
 * seller : {"description":"我是商家2","icon":"http://120.27.23.105/images/icon.png","name":"商家2","productnums":999,"score":5,"sellerid":2}
 * code : 0
 * data : {"bargainprice":111.99,"createtime":"2017-10-14t21:48:08","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","itemtype":2,"pid":9,"price":78.99,"pscid":1,"salenum":2356,"sellerid":2,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}
 */
 
 private string msg;
 private sellerbean seller;
 private string code;
 private databean data;
 
 public string getmsg() {
 return msg;
 }
 
 public void setmsg(string msg) {
 this.msg = msg;
 }
 
 public sellerbean getseller() {
 return seller;
 }
 
 public void setseller(sellerbean seller) {
 this.seller = seller;
 }
 
 public string getcode() {
 return code;
 }
 
 public void setcode(string code) {
 this.code = code;
 }
 
 public databean getdata() {
 return data;
 }
 
 public void setdata(databean data) {
 this.data = data;
 }
 
 public static class sellerbean {
 /**
 * description : 我是商家2
 * icon : http://120.27.23.105/images/icon.png
 * name : 商家2
 * productnums : 999
 * score : 5.0
 * sellerid : 2
 */
 
 private string description;
 private string icon;
 private string name;
 private int productnums;
 private double score;
 private int sellerid;
 
 public string getdescription() {
 return description;
 }
 
 public void setdescription(string description) {
 this.description = description;
 }
 
 public string geticon() {
 return icon;
 }
 
 public void seticon(string icon) {
 this.icon = icon;
 }
 
 public string getname() {
 return name;
 }
 
 public void setname(string name) {
 this.name = name;
 }
 
 public int getproductnums() {
 return productnums;
 }
 
 public void setproductnums(int productnums) {
 this.productnums = productnums;
 }
 
 public double getscore() {
 return score;
 }
 
 public void setscore(double score) {
 this.score = score;
 }
 
 public int getsellerid() {
 return sellerid;
 }
 
 public void setsellerid(int sellerid) {
 this.sellerid = sellerid;
 }
 }
 
 public static class databean {
 /**
 * bargainprice : 111.99
 * createtime : 2017-10-14t21:48:08
 * 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
 * itemtype : 2
 * pid : 9
 * price : 78.99
 * pscid : 1
 * salenum : 2356
 * sellerid : 2
 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下
 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g
 */
 
 private double bargainprice;
 private string createtime;
 private string detailurl;
 private string images;
 private int itemtype;
 private int pid;
 private double price;
 private int pscid;
 private int salenum;
 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 getitemtype() {
 return itemtype;
 }
 
 public void setitemtype(int itemtype) {
 this.itemtype = itemtype;
 }
 
 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 getsalenum() {
 return salenum;
 }
 
 public void setsalenum(int salenum) {
 this.salenum = salenum;
 }
 
 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;
 }
 }
}

加入购物车的bean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class addcart {
 private string msg;
 private string code;
 
 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;
 }
}

商品类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
public class cartbean {
 /**
 * msg : 请求成功
 * code : 0
 * data : [{"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":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainprice":11800,"createtime":"2017-10-10t17:33:37","detailurl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=qqfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 mx150 2g显存】全高清窄边框 8g内存 256固态硬盘 支持指纹识别 预装win10系统","title":"小米(mi)air 13.3英寸全金属轻薄笔记本(i5-7200u 8g 256g pcle ssd mx150 2g独显 fhd 指纹识别 win10)银 "},{"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":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":5,"pid":12,"price":256,"pscid":1,"selected":0,"sellerid":5,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellername":"商家5","sellerid":"5"},{"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":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)"}],"sellername":"商家7","sellerid":"7"},{"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":12,"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":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":76,"price":37999.99,"pscid":40,"selected":0,"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":1,"pid":5,"price":88.99,"pscid":1,"selected":0,"sellerid":21,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellername":"商家21","sellerid":"21"}]
 */
 
 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":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":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainprice":11800,"createtime":"2017-10-10t17:33:37","detailurl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=qqfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 mx150 2g显存】全高清窄边框 8g内存 256固态硬盘 支持指纹识别 预装win10系统","title":"小米(mi)air 13.3英寸全金属轻薄笔记本(i5-7200u 8g 256g pcle ssd mx150 2g独显 fhd 指纹识别 win10)银 "},{"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;
 
 private boolean check;
 
 public boolean ischeck() {
 return check;
 }
 
 public void setcheck(boolean check) {
 this.check = check;
 }
 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 : 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 : 24
 * price : 288.0
 * pscid : 2
 * selected : 0
 * sellerid : 1
 * subhead : 三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》
 * title : 三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋
 */
 
 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 int cou=1;
 
 public int getcou() {
 return cou;
 }
 
 public void setcou(int cou) {
 this.cou = cou;
 }
 
 private boolean check;
 
 public boolean ischeck() {
 return check;
 }
 
 public void setcheck(boolean check) {
 this.check = check;
 }
 
 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;
 }
 }
 }
}

app

?
1
2
3
4
5
6
7
8
public class myapp extends application{
 @override
 public void oncreate() {
 super.oncreate();
 
 fresco.initialize(this);
 }
}

view层

先写两个接口

?
1
2
3
public interface icart {
 void showlist(list<cartbean.databean> grouplist, list<list<cartbean.databean.listbean>> childlist);
}
?
1
2
3
4
public interface iview<t> {
 void showbean(t t);
 void showstr(string s);
}

主页面布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.bwie.zhangzhen20170108.mainactivity">
 
 <expandablelistview
 android:id="@+id/elv"
 android:layout_weight="1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <linearlayout
 android:layout_weight="9"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <checkbox
 android:id="@+id/quanxuan"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <textview
 android:text="全选"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <linearlayout
 android:layout_marginleft="50dp"
 android:orientation="vertical"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <textview
 android:id="@+id/zongjia"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="总价:0.0"
 />
 <textview
 android:text="共0件商品"
 android:id="@+id/tv_count"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 
 </linearlayout>
 <textview
 android:text="去结算"
 android:background="#ffd82020"
 android:layout_marginleft="100dp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 </linearlayout>
 
</linearlayout>

主页面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class mainactivity extends appcompatactivity implements icart {
 
 private expandablelistview melv;
 private checkbox mquanxuan;
 private textview mzongjia;
 private textview mtvcount;
 private myexpandableadapter myexpandableadapter;
 
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 
 initview();
 new cartpresenter(this).getcart();
 eventbus.getdefault().register(this);
 mquanxuan.setonclicklistener(new view.onclicklistener() {
 @override
 public void onclick(view v) {
 myexpandableadapter.qx(mquanxuan.ischecked());
 }
 });
 }
 
 private void initview() {
 melv = (expandablelistview) findviewbyid(r.id.elv);
 mquanxuan = (checkbox) findviewbyid(r.id.quanxuan);
 mzongjia = (textview) findviewbyid(r.id.zongjia);
 mtvcount = (textview) findviewbyid(r.id.tv_count);
 }
 
 @override
 public void showlist(list<cartbean.databean> grouplist, list<list<cartbean.databean.listbean>> childlist) {
 myexpandableadapter = new myexpandableadapter(grouplist, this,childlist);
 melv.setadapter(myexpandableadapter);
 for (int i = 0; i <grouplist.size() ; i++) {
 melv.expandgroup(i);
 }
 }
 
 //接收传过来的值
 @subscribe
 public void onpriceandcount(countandprice cp){
 
 
 mzongjia.settext("共"+cp.getcount()+"件商品");
 mtvcount.settext("总计:"+cp.getprice());
 
 }
 //接收传过来的值
 @subscribe
 public void onpriceandcount1(messgeevent event){
 //改变全选的状态
 mquanxuan.setchecked(event.ischeck());
 }
 @override
 protected void ondestroy() {
 super.ondestroy();
 eventbus.getdefault().unregister(this);
 }
}

第二个页面布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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"
 android:id="@+id/activity_add"
 tools:context="com.bwie.zhangzhen20170108.addactivity">
 
 <com.facebook.drawee.view.simpledraweeview
 android:layout_width="300dp"
 android:id="@+id/xqsdv1"
 android:layout_gravity="center"
 android:layout_height="300dp" />
 <textview
 android:id="@+id/tvtitle1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 <linearlayout
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">
 <button
 android:id="@+id/tiaozhuan1"
 android:layout_weight="1"
 android:text="跳转到购物车"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 <button
 android:id="@+id/tianjia1"
 android:layout_weight="1"
 android:text="添加到购物车"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 </linearlayout>
</linearlayout>

实现购物车的代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class addactivity extends appcompatactivity implements view.onclicklistener,iview {
 
 private simpledraweeview mxqsdv;
 private textview mtvtitle;
 private button mtiaozhuan;
 private button mtianjia;
 private linearlayout maddactivity;
 private xqpresenter xqpresenter;
 
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_add);
 
 initview();
 xqpresenter=new xqpresenter(this);
 xqpresenter.getxq();
 }
 
 private void initview() {
 mxqsdv = (simpledraweeview) findviewbyid(r.id.xqsdv1);
 mtvtitle = (textview) findviewbyid(r.id.tvtitle1);
 mtiaozhuan = (button) findviewbyid(r.id.tiaozhuan1);
 mtiaozhuan.setonclicklistener(this);
 mtianjia = (button) findviewbyid(r.id.tianjia1);
 mtianjia.setonclicklistener(this);
 maddactivity = (linearlayout) findviewbyid(r.id.activity_add);
 }
 
 @override
 public void onclick(view v) {
 switch (v.getid()) {
 default:
 break;
 
 //跳到mainactivity (购物车)
 case r.id.tiaozhuan1:
 startactivity(new intent(this,mainactivity.class));
 break;
 case r.id.tianjia1:
 xqpresenter.getadd();
 break;
 }
 }
 
 @override
 public void showbean(object o) {
 xqbean xqbean = (xqbean) o;
 xqbean.databean data = xqbean.getdata();
 string s = data.getimages().split("|")[0];
 uri uri =uri.parse(s);//地址转换成uri
 mxqsdv.setimageuri(uri);
 mtvtitle.settext(data.gettitle()+" "+data.getprice());
 }
 
 @override
 public void showstr(string s) {
 toast.maketext(this, s, toast.length_short).show();
 }
}

eventbus包

设置一个数量和价格的类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class countandprice {
 private int count ;
 private int price;
 
 public int getcount() {
 return count;
 }
 
 public void setcount(int count) {
 this.count = count;
 }
 
 public int getprice() {
 return price;
 }
 
 public void setprice(int price) {
 this.price = price;
 }
}

eventbus

?
1
2
3
4
5
6
7
8
9
10
11
public class messgeevent {
 private boolean check;
 
 public boolean ischeck() {
 return check;
 }
 
 public void setcheck(boolean check) {
 this.check = check;
 }
}

model

实现详情页model层的接口

?
1
2
3
public interface ixqmodel {
 public void getxq(int pid, string str, final onnetlisenter<xqbean> cgsb);
}

实现详情页model

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class xqmodel implements ixqmodel{
 
 @override
 public void getxq(int pid, string str, final onnetlisenter<xqbean> onnetlisenter) {
 flowable<xqbean> flowable = retrofithepler.getserviceapi().getxq(pid, str);
 flowable.observeon(androidschedulers.mainthread())
 .subscribeon(schedulers.io())
 .subscribe(new consumer<xqbean>() {
  @override
  public void accept(xqbean xqbean) throws exception {
  onnetlisenter.success(xqbean);
  }
 });
 }
 
 public void getadd(int uid, int pid, string str, final onnetlisenter<addcart> onnetlisenter){
 flowable<addcart> flowable = retrofithepler.getserviceapi().getmsg(uid, pid, str);
 flowable.observeon(androidschedulers.mainthread())
 .subscribeon(schedulers.io())
 .subscribe(new consumer<addcart>() {
  @override
  public void accept(addcart addcart) throws exception {
  onnetlisenter.success(addcart);
  }
 });
 }
}

购物车的model

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class cartmodel {
 public void getcart(int uid, string str, final onnetlisenter<cartbean> onnetlisenter){
 flowable<cartbean> flowable = retrofithepler.getserviceapi().getcart(uid,str);
 flowable.observeon(androidschedulers.mainthread())
 .subscribeon(schedulers.io())
 .subscribe(new consumer<cartbean>() {
  @override
  public void accept(cartbean cartbean) throws exception {
  onnetlisenter.success(cartbean);
  }
 });
 }
}

presenter层

详情页的p层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class xqpresenter {
 private final xqmodel xqmodel;
 private iview iview;
 public xqpresenter(iview iview) {
 xqmodel = new xqmodel();
 this.iview=iview;
 }
 public void getxq(){
 xqmodel.getxq(12, "android", new onnetlisenter<xqbean>() {
 @override
 public void success(xqbean xqbean) {
 iview.showbean(xqbean);
 }
 });
 }
 public void getadd(){
 xqmodel.getadd(3043, 12, "android", new onnetlisenter<addcart>() {
 @override
 public void success(addcart addcart) {
 iview.showstr(addcart.getmsg());
 }
 });
 }
}

购物车的p层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class cartpresenter {
 private icart icart;
 private final cartmodel cartmodel;
 
 public cartpresenter(icart icart) {
 this.icart = icart;
 cartmodel = new cartmodel();
 
 }
 public void getcart(){
 cartmodel.getcart(3043,"android",new onnetlisenter<cartbean>(){
 
 @override
 public void success(cartbean cartbean) {
 list<cartbean.databean> grouplist = cartbean.getdata();
 list<list<cartbean.databean.listbean>> childlist = new arraylist<>();
 for (int i = 0; i < grouplist.size(); i++) {
  cartbean.databean databean = grouplist.get(i);
  list<cartbean.databean.listbean> list = databean.getlist();
  childlist.add(list);
 }
 icart.showlist(grouplist,childlist);
 }
 });
 }
}

adapter

public class myexpandableadapter extends baseexpandablelistadapter {

private list<cartbean.databean> groupdata;
private context context;
private list<list<cartbean.databean.listbean>> childlist;

public myexpandableadapter( list<cartbean.databean> groupdata, context context, list<list<cartbean.databean.listbean>> childlist) {
this.groupdata = groupdata;
this.context = context;
this.childlist = childlist;
}

@ override
public int getgroupcount() {
return groupdata.size();
}

@ override
public int getchildrencount(int groupposition) {
return childlist.get(groupposition).size();
}

@override
public object getgroup(int groupposition) {
return groupdata.get(groupposition);
}

@override
public object getchild(int groupposition, int childposition) {
return null;
}

@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 holder;
if (convertview == null) {
convertview = view.inflate(context, r.layout.group_item, null);
holder = new groupviewholder();
holder.tv_group = (textview) convertview.findviewbyid(r.id.tv_group);
holder.group_cb = (checkbox) convertview.findviewbyid(r.id.gx_group);

convertview.settag(holder);
} else {
holder = (groupviewholder) convertview.gettag();
}
final cartbean.databean databean = groupdata.get(groupposition);

string sellername = databean.getsellername();
holder.group_cb.setchecked(databean.ischeck());
//点击一级checkbox的状态
holder.group_cb.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
databean.setcheck(holder.group_cb.ischecked());
//改变所有孩子的状态
changechildstate(groupposition,holder.group_cb.ischecked());
eventbus.getdefault().post(js());
//通过判断一级的checkbox判断全选的状态
changemianqxstatus(checkgroupall());
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) {
convertview = view.inflate(context, r.layout.child_item, null);
holder = new childviewholder();

holder.tv_title= (textview) convertview.findviewbyid(r.id.title);
holder.price= (textview) convertview.findviewbyid(r.id.price);
holder.child_cb= (checkbox) convertview.findviewbyid(r.id.gouxuan_child);
holder.add= (textview) convertview.findviewbyid(r.id.jia);
holder.del= (textview) convertview.findviewbyid(r.id.del);
holder.jian= (textview) convertview.findviewbyid(r.id.jian);
holder.sdv= (simpledraweeview) convertview.findviewbyid(r.id.title_img);
holder.tv_num= (textview) convertview.findviewbyid(r.id.tv_num);
holder.yanse= (textview) convertview.findviewbyid(r.id.yanse);
convertview.settag(holder);
} else {
holder = (childviewholder) convertview.gettag();
}
final cartbean.databean.listbean listbean = childlist.get(groupposition).get(childposition);
int price = (int) listbean.getprice();
holder.child_cb.setchecked(listbean.ischeck());
holder.tv_title.settext(title);
string subhead = listbean.getsubhead();
string substring = subhead.substring(1, 10)+".....";
holder.yanse.settext(substring);
string images = listbean.getimages();
string[] split = images.split("|");
string s = split[0];
uri uri=uri.parse(s);
holder.sdv.setimageuri(uri);
// listbean.setcou(integer.parseint(holder.tv_num.gettext().tostring()));
//给子条目的checkbox点击事件
holder.child_cb.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
listbean.setcheck(holder.child_cb.ischecked());

eventbus.getdefault().post(js());
//如果二级的checkbox选中,就让一级的checkbox勾选
if(holder.child_cb.ischecked()) {

  if (ischeckallchild(groupposition)) {
  //改变一级checkbox的状态
  changegroupstatus(groupposition,true);
  //通过判断一级的checkbox的状态来改变全选的状态
  changemianqxstatus(checkgroupall());

  }
}else{
  changegroupstatus(groupposition,false);
  changemianqxstatus(checkgroupall());
}
notifydatasetchanged();
}

});

//点击加加的功能
holder.add.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
int cou = listbean.getcou();
holder.tv_num.settext(++cou+"");
listbean.setcou(cou);
if(holder.child_cb.ischecked()){
  eventbus.getdefault().post(js());
}

}
});
//点击减减的功能
holder.jian.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
int cou = listbean.getcou();

if(cou==1) {
  toast.maketext(context, "最小数量为1", toast.length_short).show();
  return;}
holder.tv_num.settext(--cou+"");
listbean.setcou(cou);
if (holder.child_cb.ischecked()) {
  eventbus.getdefault().post(js());

}
}
});
//点击删除
holder.del.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
list<cartbean.databean.listbean> listbeen = childlist.get(groupposition);

cartbean.databean.listbean remove = listbeen.remove(childposition);
if(listbeen.size()==0){
  childlist.remove(childposition);
  groupdata.remove(groupposition);
}
eventbus.getdefault().post(js());
notifydatasetchanged();

}
});

return convertview;
}

@override
public boolean ischildselectable(int groupposition, int childposition) {
return true;
}

class groupviewholder {
checkbox group_cb;
textview tv_group;
}

class childviewholder {
checkbox child_cb;
textview tv_title,price,yanse,tv_num;
textview del,add,jian;
simpledraweeview sdv;


}
/**
* 判断全部的二级是checkbox否选中
*
*/
private boolean ischeckallchild(int g){
list<cartbean.databean.listbean> listbeen = childlist.get(g);

for (int i = 0; i <listbeen.size() ; i++) {
cartbean.databean.listbean listbean = listbeen.get(i);

if(!listbean.ischeck()){
return false;
}
}
return true;
}

/**
* 计算数量和价格
* 通过遍历一级的得到二级的checkbox,如果是选择的选中状态的话,就统计它们的总计的数量和价格
*/
private countandprice js(){
int cou=0;
int jiage=0;
for (int i = 0; i <groupdata.size() ; i++) {
for (int j = 0; j <childlist.get(i).size() ; j++) {
cartbean.databean.listbean listbean = childlist.get(i).get(j);
if(listbean.ischeck()){
  cou+=listbean.getcou();
  jiage+=(int)listbean.getprice()*listbean.getcou();
}

}

}
countandprice countandprice = new countandprice();
countandprice.setcount(cou);
countandprice.setprice(jiage);
return countandprice;
}
/**
* 改变一级列表的状态
*/
private void changegroupstatus(int groupposition,boolean flag ){
cartbean.databean databean = groupdata.get(groupposition);

databean.setcheck(flag);
}
/**
* 改变全选的状态
*
*/
private void changemianqxstatus(boolean flag){
messgeevent messgeevent = new messgeevent();
messgeevent.setcheck(flag);
eventbus.getdefault().post(messgeevent);
}
/**
* 判断一级的是否全部选中
*/
private boolean checkgroupall(){
for (int i = 0; i < groupdata.size(); i++) {
if(!groupdata.get(i).ischeck()){
return false;
}
}
return true;
}
/**
* 改变所有孩子的状态
*/
private void changechildstate(int groupistion,boolean flag){
list<cartbean.databean.listbean> listbeen = childlist.get(groupistion);
for (int i = 0; i < listbeen.size(); i++) {
listbeen.get(i).setcheck(flag);
}
}
/**
* 改变全选的
*/
public void qx(boolean flag){
for (int i = 0; i < groupdata.size(); i++) {
changegroupstatus(i, flag);
changechildstate(i, flag);
}
eventbus.getdefault().post(js());
notifydatasetchanged();
}
}

group_item.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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="match_parent"
 android:orientation="horizontal">
 
 <checkbox
 android:id="@+id/gx_group"
 android:focusable="false"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <textview
 android:id="@+id/tv_group"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</linearlayout>

child_item.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?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="match_parent"
 android:orientation="vertical">
 
 <textview
 android:id="@+id/title"
 android:layout_marginleft="20dp"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 <linearlayout
 android:orientation="horizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <checkbox
 android:layout_marginleft="20dp"
 android:gravity="center_vertical"
 android:id="@+id/gouxuan_child"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <com.facebook.drawee.view.simpledraweeview
 android:gravity="center_vertical"
 android:id="@+id/title_img"
 android:layout_width="50dp"
 android:layout_height="50dp" />
 <linearlayout
 android:orientation="vertical"
 android:layout_marginleft="30dp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <textview
 android:id="@+id/price"
 android:layout_marginleft="10dp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <textview
 android:id="@+id/yanse"
 android:layout_marginleft="10dp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <linearlayout
 android:layout_marginleft="100dp"
 android:orientation="horizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <textview
  android:id="@+id/jian"
  android:layout_width="20dp"
  android:layout_height="20dp"
  android:textsize="15sp"
  android:text="一"/>
 
 <textview
  android:id="@+id/tv_num"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginleft="5dp"
  android:paddingbottom="2dp"
  android:paddingleft="20dp"
  android:paddingright="20dp"
  android:paddingtop="2dp"
  android:text="1" />
 
 <textview
  android:id="@+id/jia"
  android:layout_width="20dp"
  android:layout_height="20dp"
  android:layout_marginleft="5dp"
  android:text="+"
  android:textsize="15sp"/>
 </linearlayout>
 
 </linearlayout>
 <textview
 android:id="@+id/del"
 android:layout_marginleft="20dp"
 android:text="删除"
 android:textcolor="#ffffff"
 android:background="#ff3660"
 android:layout_width="32dp"
 android:layout_height="29dp" />
 </linearlayout>
</linearlayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Cyq_0927/article/details/79006812

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部