{"id":136,"date":"2023-02-18T19:44:42","date_gmt":"2023-02-18T11:44:42","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=136"},"modified":"2023-02-18T19:44:55","modified_gmt":"2023-02-18T11:44:55","slug":"called-attach-on-a-child-which-is-not-detached-viewholder","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/called-attach-on-a-child-which-is-not-detached-viewholder\/","title":{"rendered":"Called attach on a child which is not detached ViewHolder\u95ee\u9898\u89e3\u51b3"},"content":{"rendered":"<pre><code>java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder ...<\/code><\/pre>\n<p>\u5728\u4f7f\u7528<code>RecyclerView<\/code>\u66f4\u65b0\u67d0\u4e2aitem\u7684\u6570\u636e\uff0c\u8c03\u7528<code>notifyItemChanged()<\/code>\u7684\u65f6\u5019\u51fa\u73b0\u7684\u8fd9\u4e2a\u95ee\u9898\u3002\u5177\u4f53\u4e5f\u6ca1\u6709\u62a5\u662f\u54ea\u4e00\u884c\u7684\u95ee\u9898\uff0c\u6839\u636e\u9519\u8bef\u63d0\u793a\uff0c\u610f\u601d\u662f\u64cd\u4f5c\u7684\u8fd9\u4e2a<code>ViewHolder<\/code>\u5f53\u524d\u4e0d\u662f\u88ab\u7ed1\u5b9a\u7684\uff0c\u56e0\u4e3a<code>RecyclerView<\/code>\u6709\u7f13\u5b58\u673a\u5236\uff0c\u672a\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u7684item\u4f1a\u88ab\u6682\u65f6\u56de\u6536\uff0c\u5373detached\u3002<\/p>\n<p><!-- more --><\/p>\n<p>1\u3001\u51fa\u73b0\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\u662f\u66f4\u65b0\u4e86\u4e0d\u5728\u5c4f\u5e55\u4e2d\u663e\u793a\u7684item\uff0c\u89e3\u51b3\u529e\u6cd5\u662f\u5224\u65ad\u8981\u66f4\u65b0\u7684item\u662f\u4e0d\u662f\u5728\u5c4f\u5e55\u4e2d\uff0c\u5224\u65ad\u65b9\u6cd5\u662f\u83b7\u53d6<code>RecyclerView<\/code>\u7684<code>LayoutManager<\/code>\uff0c\u524d\u63d0\u662f<code>RecyclerView<\/code>\u8bbe\u7f6e\u7684<code>LayoutManager<\/code>\u662f<code>LinearLayoutManager<\/code>\u3002\u83b7\u53d6\u7b2c\u4e00\u4e2a\u53ef\u89c1\u4f4d\u7f6e\u548c\u6700\u540e\u4e00\u4e2a\u53ef\u89c1\u4f4d\u7f6e\u7684position\uff0c\u5224\u65ad\u5f53\u524d\u8981\u66f4\u65b0\u7684item\u7684position\u5728\u8fd9\u4e2a\u8303\u56f4\u5185\u624d\u66f4\u65b0\u3002<\/p>\n<pre><code class=\"language-java\">LinearLayoutManager linearManager = (LinearLayoutManager) recyclerView.getLayoutManager();\n\/\/\u7b2c\u4e00\u4e2a\u53ef\u89c1View\u7684\u4f4d\u7f6e\nint mFirstVisibleItemPosition = linearManager.findFirstVisibleItemPosition();\n\/\/\u6700\u540e\u4e00\u4e2a\u53ef\u89c1View\u7684\u4f4d\u7f6e\nint mLastVisibleItemPosition = linearManager.findLastVisibleItemPosition();<\/code><\/pre>\n<p>2\u3001\u4f46\u662f\u5982\u679c\u5217\u8868\u53ea\u6709\u4e00\u6761\u6570\u636e\uff0c\u5e76\u4e14\u662f\u5728\u5c4f\u5e55\u4e2d\u663e\u793a\u7684\uff0c\u5728<code>notifyItemChanged(0)<\/code>\u8fd8\u662f\u62a5\u4e86\u8fd9\u4e2a\u9519\uff0c\u7ecf\u8fc7\u4ed4\u7ec6\u7814\u7a76\u4ee3\u7801\uff0c\u53d1\u73b0<code>RecyclerView<\/code>\u6dfb\u52a0\u4e86<code>Header<\/code>\uff0c\u8fd9\u6837<code>position=0<\/code>\u7684\u4f4d\u7f6e\u5176\u5b9e\u662f<code>Header<\/code>\uff0c\u6240\u4ee5\u51fa\u73b0\u4e86\u8fd9\u6837\u7684\u95ee\u9898\u3002\u6240\u4ee5\u66f4\u65b0List\u4e2dposition\u4f4d\u7f6e\u7684\u6570\u636e\u540e\u8981\u66f4\u65b0<code>Adapter<\/code>\u7684\u4f4d\u7f6e\u662f<code>position+1<\/code>\uff0c\u6539\u6210<code>notifyItemChanged(1)<\/code>\u4e4b\u540e\u95ee\u9898\u89e3\u51b3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java.lang.IllegalArgumentException: Called attach on a  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[65],"class_list":["post-136","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-recyclerview"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/136","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=136"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}