From 84b4656cfdd26dceeffb36e995dbc0055db68476 Mon Sep 17 00:00:00 2001 From: pokercc Date: Sat, 10 Oct 2020 18:42:08 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20=E2=80=9Cjava.lang.IllegalArgumentExcepti?= =?UTF-8?q?on:=20Called=20attach=20on=20a=20child=20which=20is=20not=20det?= =?UTF-8?q?ached"=20when=20notifyItemChanged(0)=20with=20headerView.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chad/library/adapter/base/BaseQuickAdapter.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt index 8fb583f05..120610112 100644 --- a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt +++ b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt @@ -661,7 +661,17 @@ abstract class BaseQuickAdapter * @return VH */ @Suppress("UNCHECKED_CAST") - protected open fun createBaseViewHolder(view: View): VH { + protected open fun createBaseViewHolder(originView: View): VH { + (originView.parent as? ViewGroup)?.removeView(originView) + // Fix “java.lang.IllegalArgumentException: Called attach on a child which is not detached" when notifyItemChanged(0) with headerView. + // Create a wrapper viewGroup will create new layoutParams and reuse headerView. + // Avoid RecyclerView.getChildViewHolderInt() return the old viewHolder which occur crash. + // Link to https://github.com/CymChad/BaseRecyclerViewAdapterHelper/issues/2907 + val view = FrameLayout(originView.context) + view.addView(originView) + view.layoutParams = RecyclerView.LayoutParams(originView.layoutParams) + + // Create ViewHolder var temp: Class<*>? = javaClass var z: Class<*>? = null while (z == null && null != temp) {