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

}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/11/android-recyclerview-mvp-architecture-refresh-and-load-more-data/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android RecyclerView MVP架构刷新及加载更多数据
Android RecyclerView MVP架构刷新及加载更多数据 View public void loadDocumentList(boolean increment) { mPresenter.loadDocumentList(mDocumentList……
<<上一篇
下一篇>>
文章目录
关闭
目 录