本文实例为大家分享了rxjava retrofit实现购物车展示的具体代码,供大家参考,具体内容如下
先给大家展示一下效果图
框架结构:
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
|