Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix “java.lang.IllegalArgumentException: Called attach on a child whi… #3341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,17 @@ abstract class BaseQuickAdapter<T, VH : BaseViewHolder>
* @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) {
Expand Down