{"id":937,"date":"2023-03-11T18:45:01","date_gmt":"2023-03-11T10:45:01","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=937"},"modified":"2023-04-29T15:43:24","modified_gmt":"2023-04-29T07:43:24","slug":"android-recyclerview-mvp-architecture-refresh-and-load-more-data","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-recyclerview-mvp-architecture-refresh-and-load-more-data\/","title":{"rendered":"Android RecyclerView MVP\u67b6\u6784\u5237\u65b0\u53ca\u52a0\u8f7d\u66f4\u591a\u6570\u636e"},"content":{"rendered":"<p>Android RecyclerView MVP\u67b6\u6784\u5237\u65b0\u53ca\u52a0\u8f7d\u66f4\u591a\u6570\u636e<\/p>\n<h2>View<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public void loadDocumentList(boolean increment) {\n    mPresenter.loadDocumentList(mDocumentList, increment);\n}\n\n@Override\npublic void showProgress() {\n    LoadingDialog.show(this);\n}\n\n@Override\npublic void onLoadDocumentListSuccess(List&lt;Document&gt; documentList) {\n    LoadingDialog.dismiss(this);\n    mAdapter.notifyDataSetChanged();\n}\n\n@Override\npublic void onLoadDocumentListError(ErrMsg errMsg) {\n    LoadingDialog.dismiss(this);\n}<\/code><\/pre>\n<h2>Presenter<\/h2>\n<pre><code class=\"language-java\">public class MainPresenter extends AbsMainPresenter {\n\n    @Override\n    public void loadDocumentList(final List&lt;Document&gt; documentList, boolean increment) {\n        int count = 10;\n        if (increment) {\n            count += documentList.size();\n        }\n        String url = String.format(HttpServer.DOCUMENT_LIST_URL, 0, count);\n        mainView = mViewRef.get();\n        mainView.showProgress();\n        HttpManager.getInstance().get().url(url)\n                \/\/.headers(HttpParamsBuilder.buildAuthorizedCommonRequestHeaderParams((Context) mainView))\n                .build().enqueue(new Callback() {\n            @Override\n            public Object onNetworkResponse(Response response) throws Exception {\n                ResponseBody body = response.body();\n                if (body != null) {\n                    String result = body.string();\n                    NLog.i(TAG, result);\n                    body.close();\n                    JSONObject json = JSON.parseObject(result);\n                    if (json.getInteger(&quot;code&quot;) == 0) {\n                        JSONObject data = json.getJSONObject(&quot;data&quot;);\n                        List&lt;Document&gt; list = JsonHelper.jsonToList(data.getString(&quot;document_list&quot;), Document.class);\n                        if (list != null &amp;&amp; list.size() &gt; 0) {  \/\/\u6570\u636e\u66f4\u65b0\n                            documentList.clear();\n                            documentList.addAll(list);\n                        } else {  \/\/\u6570\u636e\u7a7a\n                            documentList.clear();\n                        }\n                        return documentList;\n                    }\n                }\n                return null;\n            }\n\n            @Override\n            public void onError(Call call, int httpCode, Exception e) {\n                if (httpCode &gt;= 400 &amp;&amp; httpCode &lt; 500) {\n                    mainView.onLoadDocumentListError(new ErrMsg(ErrorCode.NETWORK_ERROR, ((Context) mainView).getResources().getString(R.string.network_error)));\n                    return;\n                }\n                String result = e.getLocalizedMessage();\n                NLog.i(TAG, result);\n                try {\n                    JSONObject json = JSON.parseObject(result);\n                    if (json != null) {\n                        mainView.onLoadDocumentListError(new ErrMsg(json.getInteger(&quot;code&quot;), json.getString(&quot;msg&quot;)));\n                    } else {\n                        mainView.onLoadDocumentListError(new ErrMsg(ErrorCode.NETWORK_ERROR, ((Context) mainView).getResources().getString(R.string.network_error)));\n                    }\n                } catch (JSONException jsonException) {\n                    mainView.onLoadDocumentListError(new ErrMsg(ErrorCode.NETWORK_ERROR, ((Context) mainView).getResources().getString(R.string.network_error)));\n                }\n            }\n\n            @Override\n            public void onResponse(Object response) {\n                if (response != null) {\n                    if (response instanceof List) {\n                        List&lt;Document&gt; list = (List&lt;Document&gt;) response;\n                        mainView.onLoadDocumentListSuccess(list);\n                        return;\n                    } else if (response instanceof ErrMsg) {\n                        mainView.onLoadDocumentListError((ErrMsg) response);\n                        return;\n                    }\n                }\n                mainView.onLoadDocumentListError(new ErrMsg(ErrorCode.UNKOWN, ((Context) mainView).getResources().getString(R.string.api_document_list_get_error)));\n            }\n        });\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android RecyclerView MVP\u67b6\u6784\u5237\u65b0\u53ca\u52a0\u8f7d\u66f4\u591a\u6570\u636e View public void lo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[127,65],"class_list":["post-937","post","type-post","status-publish","format-standard","hentry","category-android-ui","tag-mvp","tag-recyclerview"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=937"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/937\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}