I\'m using a simple implementation of RecyclerView
taken from the Android website using a StaggeredGridLayoutManager
and I keep getting this error
I use com.squareup.picasso.RequestCreator
public void into(android.widget.ImageView target,
Callback callback)
to dynamically resize ImageView's size after download picture from Internet, and saves the resized width and height to preserve the view size. I got this Exception because I saved LayoutParams
in a Map
, and in my onBindViewHolder, I retrieved it and directly set it to my ImageView
. I fix this by use ImmutablePair<Integer, Integer>
to only store ImageView's size rather than a lot of other states, and use following code to restore it.
ViewGroup.LayoutParams params = image.getLayoutParams();
params.width = widthAndHeight.getLeft();
params.height = widthAndHeight.getRight();
image.setLayoutParams(params);
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.
I also encountered the same errror when scrolling on the RecyclerView
: then I removed animateLayoutChanges="true"
in layout file for RecyclerView
then everything worked.