Android RecyclerView MVP架构刷新及加载更多数据

Android RecyclerView MVP架构刷新及加载更多数据

View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void loadDocumentList(boolean increment) {
mPresenter.loadDocumentList(mDocumentList, increment);
}

@Override
public void showProgress() {
LoadingDialog.show(this);
}

@Override
public void onLoadDocumentListSuccess(List<Document> documentList) {
LoadingDialog.dismiss(this);
mAdapter.notifyDataSetChanged();
}

@Override
public void onLoadDocumentListError(ErrMsg errMsg) {
LoadingDialog.dismiss(this);
}

Presenter

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
public class MainPresenter extends AbsMainPresenter {

@Override
public void loadDocumentList(final List<Document> documentList, boolean increment) {
int count = 10;
if (increment) {
count += documentList.size();
}
String url = String.format(HttpServer.DOCUMENT_LIST_URL, 0, count);
mainView = mViewRef.get();
mainView.showProgress();
HttpManager.getInstance().get().url(url)
//.headers(HttpParamsBuilder.buildAuthorizedCommonRequestHeaderParams((Context) mainView))
.build().enqueue(new Callback() {
@Override
public Object onNetworkResponse(Response response) throws Exception {
ResponseBody body = response.body();
if (body != null) {
String result = body.string();
NLog.i(TAG, result);
body.close();
JSONObject json = JSON.parseObject(result);
if (json.getInteger("code") == 0) {
JSONObject data = json.getJSONObject("data");
List<Document> list = JsonHelper.jsonToList(data.getString("document_list"), Document.class);
if (list != null && list.size() > 0) { //数据更新
documentList.clear();
documentList.addAll(list);
} else { //数据空
documentList.clear();
}
return documentList;
}
}
return null;
}

@Override
public void onError(Call call, int httpCode, Exception e) {
if (httpCode >= 400 && httpCode < 500) {
mainView.onLoadDocumentListError(new ErrMsg(ErrorCode.NETWORK_ERROR, ((Context) mainView).getResources().getString(R.string.network_error)));
return;
}
String result = e.getLocalizedMessage();
NLog.i(TAG, result);
try {
JSONObject json = JSON.parseObject(result);
if (json != null) {
mainView.onLoadDocumentListError(new ErrMsg(json.getInteger("code"), json.getString("msg")));
} else {
mainView.onLoadDocumentListError(new ErrMsg(ErrorCode.NETWORK_ERROR, ((Context) mainView).getResources().getString(R.string.network_error)));
}
} catch (JSONException jsonException) {
mainView.onLoadDocumentListError(new ErrMsg(ErrorCode.NETWORK_ERROR, ((Context) mainView).getResources().getString(R.string.network_error)));
}
}

@Override
public void onResponse(Object response) {
if (response != null) {
if (response instanceof List) {
List<Document> list = (List<Document>) response;
mainView.onLoadDocumentListSuccess(list);
return;
} else if (response instanceof ErrMsg) {
mainView.onLoadDocumentListError((ErrMsg) response);
return;
}
}
mainView.onLoadDocumentListError(new ErrMsg(ErrorCode.UNKOWN, ((Context) mainView).getResources().getString(R.string.api_document_list_get_error)));
}
});
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :