{"id":203900,"date":"2025-05-22T10:01:31","date_gmt":"2025-05-22T02:01:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203900\/"},"modified":"2025-05-22T10:01:31","modified_gmt":"2025-05-22T02:01:31","slug":"android-recyclerview%e6%95%b0%e6%8d%ae%e6%9b%b4%e6%96%b0%e5%a4%b1%e8%b4%a5%ef%bc%8c%e8%a7%86%e5%9b%be%e4%b8%8d%e5%88%b7%e6%96%b0%e6%80%8e%e4%b9%88%e5%8a%9e%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203900\/","title":{"rendered":"Android RecyclerView\u6570\u636e\u66f4\u65b0\u5931\u8d25\uff0c\u89c6\u56fe\u4e0d\u5237\u65b0\u600e\u4e48\u529e\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Android RecyclerView\u6570\u636e\u66f4\u65b0\u5931\u8d25\uff0c\u89c6\u56fe\u4e0d\u5237\u65b0\u600e\u4e48\u529e\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aAndroid RecyclerView\u6570\u636e\u66f4\u65b0\u5931\u8d25\uff0c\u89c6\u56fe\u4e0d\u5237\u65b0\u600e\u4e48\u529e\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241130\/1732959056674adb5027261.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>android recyclerview \u5f02\u6b65\u66f4\u65b0\u6570\u636e\u89c6\u56fe\u5931\u6548<\/strong><\/p>\n<p>\u5728 android \u4e2d\uff0crecyclerview \u662f\u7528\u4e8e\u5448\u73b0\u5217\u8868\u6570\u636e\u7684\u5f3a\u5927\u7ec4\u4ef6\u3002\u4fee\u6539recyclerview\u4e2d\u6570\u636e\u540e\uff0c\u6211\u4eec\u4f1a\u5e0c\u671b\u89c6\u56fe\u76f8\u5e94\u5730\u66f4\u65b0\u3002\u4f46\u6709\u65f6\u6211\u4eec\u4f1a\u9047\u5230\u66f4\u65b0\u4e86\u6570\u636e\uff0c\u89c6\u56fe\u5374\u4e0d\u5237\u65b0\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u95ee\u9898\u539f\u56e0\u548c\u89e3\u51b3\u529e\u6cd5\uff1a<\/strong><\/p>\n<p><strong>1. \u7f3a\u5c11 notifydatasetchanged()<\/strong><\/p>\n<p>\u5f53\u6570\u636e\u53d1\u751f\u66f4\u6539\u65f6\uff0c\u6211\u4eec\u5fc5\u987b\u8c03\u7528 notifydatasetchanged() \u65b9\u6cd5\u901a\u77e5recyclerview\u66f4\u65b0\u89c6\u56fe\u3002\u5728\u7ed9\u51fa\u7684\u4ee3\u7801\u4e2d\uff0c\u867d\u7136\u4f7f\u7528\u4e86 addall() \u548c resetall() \u65b9\u6cd5\u6765\u66f4\u65b0\u6570\u636e\uff0c\u4f46\u7f3a\u5c11\u4e86\u8fd9\u4e00\u5173\u952e\u6b65\u9aa4\uff1a<\/p>\n<pre>if (append) {\n    adapter.addall(items);\n} else {\n    adapter.resetall(items);\n}\nadapter.notifydatasetchanged(); \/\/ \u6dfb\u52a0\u8fd9\u4e00\u884c\u4ee3\u7801<\/pre>\n<p><strong>2. \u6570\u636e\u96c6\u5408\u4e3a\u7a7a<\/strong><\/p>\n<p>\u5982\u679c\u7528\u6765\u66f4\u65b0recyclerview\u7684\u6570\u636e\u96c6\u5408 items \u4e3a\u7a7a\uff0c\u5219\u5373\u4f7f\u8c03\u7528\u4e86\u66f4\u65b0\u65b9\u6cd5\uff0c\u4e5f\u4e0d\u4f1a\u89e6\u53d1\u66f4\u65b0\u3002\u5728\u66f4\u65b0\u6570\u636e\u524d\uff0c\u68c0\u67e5 items \u662f\u5426\u4e3a\u7a7a\uff1a<\/p>\n<pre>if (items.isempty()) {\n    return;\n}<\/pre>\n<p><strong>3. \u6570\u636e\u8bbe\u7f6e\u4e0d\u5b8c\u6574<\/strong><\/p>\n<p>\u6bcf\u4e2a item \u5bf9\u8c61\u90fd\u5fc5\u987b\u88ab\u6b63\u786e\u5730\u8bbe\u7f6e\u6570\u636e\u3002\u5728\u7ed9\u51fa\u7684\u4ee3\u7801\u4e2d\uff0c\u53ea\u6709 item.setcover(userarchive.getcover()) \u88ab\u8c03\u7528\uff0c\u4f46\u5176\u4ed6\u6570\u636e\u6ca1\u6709\u88ab\u8bbe\u7f6e\u3002\u8bf7\u786e\u4fdd\u5168\u90e8\u6570\u636e\u90fd\u5df2\u8d4b\u503c\u3002<\/p>\n<p><strong>4. \u7f51\u7edc\u8bf7\u6c42\u5728\u4e3b\u7ebf\u7a0b\u5916\u6267\u884c<\/strong><\/p>\n<p>\u5982\u679c\u7f51\u7edc\u8bf7\u6c42\u662f\u5728\u4e3b\u7ebf\u7a0b\u5916\u6267\u884c\u7684\uff0c\u5219\u66f4\u65b0\u6570\u636e\u65f6\u9700\u8981\u4f7f\u7528 runonuithread() \u65b9\u6cd5\u786e\u4fdd\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\uff1a<\/p>\n<pre>activity.runOnUiThread(() -&gt; {\n    if (append) {\n        adapter.addAll(items);\n    } else {\n        adapter.resetAll(items);\n    }\n    adapter.notifyDataSetChanged();\n});<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aAndroid RecyclerView\u6570\u636e\u66f4\u65b0\u5931\u8d25\uff0c\u89c6\u56fe\u4e0d\u5237\u65b0\u600e\u4e48\u529e\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android Recycler&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203900","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203900"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203900\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}