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

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

View

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

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)));
            }
        });
    }

}
上一篇 Android判断应用通知权限是否开启以及跳转到设置界面
下一篇 Android Intent和PendingIntent的区别
目录
文章列表
1 Lucene的学习第一篇 — 引出Lucene
Lucene的学习第一篇 — 引出Lucene
2
搭建nodejs代理服务器解决跨域问题
搭建nodejs代理服务器解决跨域问题
3
Android RecyclerView MVP架构刷新及加载更多数据
Android RecyclerView MVP架构刷新及加载更多数据
4
Android测试(2):Android测试基础
Android测试(2):Android测试基础
5
SpringBoot-Google二步验证
SpringBoot-Google二步验证
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。