Error "You must not call setTag() on a view Glide is targeting" when use Glide
Solution 1:
The key is ViewTarget.setTagId
; setting it will free up the default setTag
on the ImageView
so you can use it as root in the item layout.
It was introduced in Glide 3.6.0 in issue #370.
In your manifest add this:
<application
android:name=".App"
Then create an application context class:
public class App extends Application {
@Override public void onCreate() {
super.onCreate();
ViewTarget.setTagId(R.id.glide_tag);
}
}
Add the following as contents for src/main/values/ids.xml
:
<resources>
<item type="id" name="glide_tag" />
</resources>
(or just add the above <item ... />
into any resources
xml in values
)
Update: ViewTarget
was deprecated since 4.8.0. If you're using into(ImageView)
, you'll still have to call the deprecated class's method, because the built-in *ViewTarget
classes still extend the old class.
If you use a custom ViewTarget
, migrate to CustomViewTarget
as the replacement. This will remove the need for setting any tag ID, because the default behaviour for CustomViewTarget
is using a Glide-specific ID, so it shouldn't clash with any setTag
calls.
If you want to customise the ID anyway, you can use useTagId
on your custom target.
Solution 2:
Glide
use setTag(Object)
method internally. So remove all calls of setTag(Object)
from your target view. ie ImageView
If you really required to use setTag
method on your view, you can use setTag(int,Object)
method instead.
Solution 3:
From what I've found from Googling, seems like Glide and ViewHolder don't get along too well. I tried Picasso instead. Problem solved instantly.
You can also try the solution from TWiStErRob, but make sure you're using Glide 3.6.0 or higher. It did not recognize setTagId(int) on 3.5.2
Solution 4:
ImageView mImageAds = (ImageView) findViewById(R.id.iv_img);
mImageAds.setTag(R.id.iv_img, i); // "i" means some integer value
Glide.with(getActivity()).load(urlPath)
.placeholder(R.drawable.ads_banner)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(mImageAds);
mImageAds.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(mImageAds.getTag() != null) {
int position = (Integer) view.getTag(R.id.iv_img);
//code here what would you like to do.
}
}
});
See also : https://androidhiker.blogspot.com/2015/10/how-to-resolve-glide-settag-issue.html