1
1
###MVCHelper主要用于下拉刷新加载,失败,加载,空数据,成功的界面切换。
2
+ MVCHelper +(IDataSource或ITask)+ IDataAdapter + 下拉刷新控件 + 布局切换(ILoadViewFactory,ILoadView,ILoadMoreView)
2
3
3
- ###TaskHelper主要用于执行多个任务,通过回调ICallback更新UI
4
+ ###TaskHelper主要用于没有布局切换和刷新控件的MVC架构,可以执行多个任务通过回调ICallback更新UI
5
+ TaskHelper+(ITask或IDataSource)+ ICallBack
4
6
5
- Download Library [ JAR] ( https://github.com/LuckyJayce/MVCHelper/releases/download/1.0.7/LuckyJayce_MVCHelper_1.0.7.zip )
6
7
Download sample [ Apk] ( https://github.com/LuckyJayce/MVCHelper/blob/master/raw/MVCHelper_Demo.apk?raw=true )
7
8
8
9
###历史版本和更新信息
@@ -12,43 +13,43 @@ https://github.com/LuckyJayce/MVCHelper/releases
12
13
## 1.必须导入: ##
13
14
14
15
//MVCHelper核心类库
15
- compile 'com.shizhefei:MVCHelper-Library:1.0.7 '
16
+ compile 'com.shizhefei:MVCHelper-Library:1.0.8 '
16
17
//里面有使用recyclerview,所以需要导入recyclerview
17
18
compile 'com.android.support:recyclerview-v7:24.0.0'
18
19
19
20
## 2.可选: ##
20
21
<1> 使用 https://github.com/chrisbanes/Android-PullToRefresh 的刷新控件导入
21
22
22
23
//里面包含一个MVCPullrefshHelper 是适配这个控件的 MVCHelper
23
- compile 'com.shizhefei:MVCHelper-Pullrefresh:1.0.7 '
24
+ compile 'com.shizhefei:MVCHelper-Pullrefresh:1.0.8 '
24
25
//由于没有找到gradle排至,我自己把它上传到jcenter上
25
26
compile 'com.shizhefei:pulltorefresh:1.0.1'
26
27
27
28
<2> 使用 https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh 的刷新控件导入
28
29
29
30
//里面包含一个MVCUltraHelper 是适配这个控件的 MVCHelper
30
- compile 'com.shizhefei:MVCHelper-UltraRefresh:1.0.7 '
31
+ compile 'com.shizhefei:MVCHelper-UltraRefresh:1.0.8 '
31
32
//这里6月29号目前最新的,要实时关注新版本去秋大的网站上去看
32
33
compile 'in.srain.cube:ultra-ptr:1.0.11'
33
34
34
35
<3> 使用android v4的SwipeRefreshLayout的作为刷新控件导入
35
36
36
37
//里面包含一个MVCSwipeRefreshHelper 是适配这个控件的 MVCHelper
37
- compile 'com.shizhefei:MVCHelper-SwipeRefresh:1.0.7 '
38
+ compile 'com.shizhefei:MVCHelper-SwipeRefresh:1.0.8 '
38
39
//v4包应该都有导入吧,v7包里面包含v4包
39
40
compile 'com.android.support:support-v4:24.0.0'
40
41
41
42
<4> 测试用例,可以方便的查看MVCHelper,Task的运行情况和返回数据,还提供了修改接口字段,用于接口测试很方便哦
42
43
43
44
//MVCHelper的测试用例,继承ABSTestCaseFragment实现List<TestCaseData> getTestCaseDatas()方法
44
- compile 'com.shizhefei:MVCHelper-TestCase:1.0.7 '
45
+ compile 'com.shizhefei:MVCHelper-TestCase:1.0.8 '
45
46
//里面用到了gson
46
47
compile 'com.google.code.gson:gson:2.2.4'
47
48
48
49
<5> MVCHelper-OkHttp 对OKHttp的简单封装
49
50
50
51
//MVCHelper的 OKHttp的简单封装
51
- compile 'com.shizhefei:MVCHelper-OkHttp:1.0.7 '
52
+ compile 'com.shizhefei:MVCHelper-OkHttp:1.0.8 '
52
53
//里面用到了okhttp3
53
54
compile 'com.squareup.okhttp3:okhttp:3.4.0'
54
55
compile 'com.squareup.okio:okio:1.9.0'
@@ -63,9 +64,11 @@ https://github.com/LuckyJayce/MVCHelper/releases
63
64
MVCHelper. 实现下拉刷新,滚动底部自动加载更多,分页加载,自动切换显示网络失败布局,暂无数据布局,,真正的MVC架构.
64
65
65
66
## 1.Model (IDataSource<DATA >)数据源,加载数据
66
- ** 同步请求实现IDataSource,异步请求(okhttp,volley)实现IAsyncDataSource**
67
+ ** 同步请求实现IDataSource,异步请求(okhttp,volley,rxjava+retrofit)实现IAsyncDataSource**
68
+
69
+ ** <1>同步请求(直接返回结果)**
67
70
68
- //数据源
71
+ //数据源
69
72
70
73
public interface IDataSource<DATA > {
71
74
// 获取刷新的数据
@@ -77,6 +80,7 @@ MVCHelper. 实现下拉刷新,滚动底部自动加载更多,分页加载,
77
80
// 是否还可以继续加载更多
78
81
public boolean hasMore();
79
82
}
83
+
80
84
例如:分页加载书籍列表数据
81
85
82
86
public class BooksDataSource implements IDataSource<List<Book >> {
@@ -109,9 +113,164 @@ MVCHelper. 实现下拉刷新,滚动底部自动加载更多,分页加载,
109
113
110
114
}
111
115
112
- 详细写法请看
116
+ ** <2>异步请求(就是请求等待回调函数返回结果)**
117
+
118
+ /**
119
+ * 异步数据源(比如Volley,OkHttp等异步请求使用)
120
+ * @param <DATA>
121
+ */
122
+ public interface IAsyncDataSource<DATA> {
123
+ /**
124
+ * 获取刷新的数据
125
+ *
126
+ * @param sender 用于请求结束时发送数据给MVCHelper,MVCHelper再通知IDataAdapter调用notifyDataChenge方法
127
+ * @return 用于提供外部取消请求的处理.比如执行refresh还没请求结束又执行refresh,就会通过上次的RequestHandle取消上次的请求.MVCHelper的destroy也会用这个取消请求
128
+ * @throws Exception
129
+ */
130
+ RequestHandle refresh(ResponseSender<DATA> sender) throws Exception;
131
+
132
+ /**
133
+ * 获取加载更多的数据
134
+ *
135
+ * @param sender 用于请求结束时发送数据给MVCHelper,MVCHelper再通知IDataAdapter调用notifyDataChenge方法
136
+ * @return 用于提供外部取消请求的处理.比如执行refresh还没请求结束又执行refresh,就会通过上次的RequestHandle取消上次的请求.MVCHelper的destroy也会用这个取消请求
137
+ * @throws Exception
138
+ */
139
+ RequestHandle loadMore(ResponseSender<DATA> sender) throws Exception;
140
+
141
+ /**
142
+ * 是否还可以继续加载更多
143
+ *
144
+ * @return
145
+ */
146
+ boolean hasMore();
147
+
148
+ }
149
+
150
+ 例如: OkHttp 请求
151
+
152
+ public class BooksOkHttpNormal_DataSource implements IAsyncDataSource<List<Book>> {
153
+ private int mPage;
154
+ private int mMaxPage = 5;
155
+
156
+ public BooksOkHttpNormal_DataSource() {
157
+ super();
158
+ }
159
+
160
+ @Override
161
+ public RequestHandle refresh(ResponseSender<List<Book>> sender) throws Exception {
162
+ return loadBooks(sender, 1);
163
+ }
164
+
165
+ @Override
166
+ public RequestHandle loadMore(ResponseSender<List<Book>> sender) throws Exception {
167
+ return loadBooks(sender, mPage + 1);
168
+ }
169
+
170
+ @Override
171
+ public boolean hasMore() {
172
+ return mPage < mMaxPage;
173
+ }
174
+
175
+ private RequestHandle loadBooks(final ResponseSender<List<Book>> sender, final int page) throws Exception {
176
+ Request request = new Request.Builder().url("https://www.baidu.com").get().build();
177
+ Call call = OkHttpUtils.client.newCall(request);
178
+ call.enqueue(new Callback() {
179
+
180
+ @Override
181
+ public void onFailure(Call call, IOException e) {
182
+ //send 要放在最后一句(请求结束)
183
+ sender.sendError(e);
184
+ }
185
+
186
+ @Override
187
+ public void onResponse(Call call, Response response) throws IOException {
188
+ final List<Book> books = new ArrayList<Book>();
189
+ for (int i = 0; i < 15; i++) {
190
+ books.add(new Book("page" + page + " Java编程思想 " + i, 108.00d));
191
+ }
192
+ mPage = page;
193
+
194
+ //send 要放在最后一句(请求结束)
195
+ sender.sendData(books);
196
+ }
197
+ });
198
+ return new OKHttpRequestHandle(call);
199
+ }
200
+ }
201
+
202
+ public class OKHttpRequestHandle implements RequestHandle {
203
+
204
+ private final Call call;
205
+
206
+ public OKHttpRequestHandle(Call call) {
207
+ super();
208
+ this.call = call;
209
+ }
210
+
211
+ @Override
212
+ public void cancle() {
213
+ call.cancel();
214
+ }
215
+
216
+ @Override
217
+ public boolean isRunning() {
218
+ return false;
219
+ }
220
+ }
221
+
222
+ 例如:使用 MVCHelper-OkHttp
223
+
224
+ public class BooksOkHttp_AsyncDataSource implements IAsyncDataSource<List<Book>> {
225
+ private int mPage;
226
+ private int mMaxPage = 5;
227
+
228
+ @Override
229
+ public RequestHandle refresh(ResponseSender<List<Book>> sender) throws Exception {
230
+ return loadBooks(sender, 1);
231
+ }
232
+
233
+ @Override
234
+ public RequestHandle loadMore(ResponseSender<List<Book>> sender) throws Exception {
235
+ return loadBooks(sender, mPage + 1);
236
+ }
237
+
238
+ @Override
239
+ public boolean hasMore() {
240
+ return mPage < mMaxPage;
241
+ }
242
+
243
+ private RequestHandle loadBooks(final ResponseSender<List<Book>> sender, final int page) throws Exception {
244
+ GetMethod method = new GetMethod("https://www.baidu.com");
245
+ method.addHeader("a", "aaaaa");
246
+ method.addParam("api_key", "75ee6c644cad38dc8e53d3598c8e6b6c");
247
+ //method 里面已经封装了sender.sendData 和 sendError的方法,只要关心ResponseParser解析Response返回数据就好
248
+ method.executeAsync(sender, new ResponseParser<List<Book>>() {
249
+ @Override
250
+ public List<Book> parse(Response response) throws Exception {
251
+ List<Book> books = new ArrayList<Book>();
252
+ for (int i = 0; i < 15; i++) {
253
+ books.add(new Book("page" + page + " Java编程思想 " + i, 108.00d));
254
+ }
255
+ mPage = page;
256
+ return books;
257
+ }
258
+ });
259
+ return method;
260
+ }
261
+ }
262
+
263
+
264
+ ** 详细写法请看
113
265
https://github.com/LuckyJayce/MVCHelper/tree/master/app/src/main/java/com/shizhefei/test/models/datasource
114
- 里面有volley,okhttp,rxjava+retrofit等使用方法
266
+ 里面有
267
+
268
+ ** 例子:[ volley] ( https://github.com/LuckyJayce/MVCHelper/tree/master/app/src/main/java/com/shizhefei/test/models/datasource/volley ) **
269
+
270
+ ** 例子:[ okhttp] ( https://github.com/LuckyJayce/MVCHelper/tree/master/app/src/main/java/com/shizhefei/test/models/datasource/okhttp ) **
271
+
272
+ ** 例子:[ rxjava+retrofit] ( https://github.com/LuckyJayce/MVCHelper/tree/master/app/src/main/java/com/shizhefei/test/models/datasource/rxjava_retrofit ) **
273
+
115
274
## 2.View(IDataAdapter<DATA >) 视图,显示数据
116
275
** 这里不是指Android的view,而是显示数据的概念和显示逻辑**
117
276
0 commit comments