RecyclerView crashes when “scrapped or attached views may not be recycled”

前端 未结 27 2832
灰色年华
灰色年华 2020-11-29 19:50

I\'m using a simple implementation of RecyclerView taken from the Android website using a StaggeredGridLayoutManager and I keep getting this error

27条回答
  •  执笔经年
    2020-11-29 20:22

    I met this issue this morning, but I am not facing the same reason as mentioned above.

    Via debug I found that the item view in my ViewHolder has mParent and it's not null, which in normal case it should be none( that's what the log saying, "attached view may not be recycled", I think it means that if the child view is already attached to a parent, it would some how cause failure when recyclering.)

    But I didn't attach the child view every time manually. And I found that it's done when I try to inflate the child view in my ViewHolder, something like:

    layoutInflater.inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
    

    And the last parameter attachToRoot should be false.

    After I change it to false, I fixed my problem.

    By the way, I only see this crash happened when I upgrade my support library to latest version 25.0.0. Before I was using version 23.4.0 and I don't see this problem happening. I guess there should be something changed in the latest support library.

    Hope this help.

提交回复
热议问题