Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "You must not call setTag() on a view Glide is targeting" when use Glide

I use Glide library inner custom adapter view in my apps. But I have Error :

"You must not call setTag() on a view Glide is targeting" 

This part of my code :

 @Override
    public View getView(int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = holder.imageView = new ImageView(context);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.imageView.setAdjustViewBounds(true);
        LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        holder.imageView .setLayoutParams(vp);
        holder.imageView .setScaleType(ImageView.ScaleType.CENTER_CROP);

        String var_news_article_images = imageIdList.get(getPosition(position));

        Glide.with(context)
                .load(var_news_article_images)
                .placeholder(R.drawable.placeholder)
               .into(holder.imageView);

               return view;
    }

so how to fix it ?

like image 935
Amay Diam Avatar asked Jan 17 '16 00:01

Amay Diam


6 Answers

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.

like image 126
TWiStErRob Avatar answered Nov 15 '22 12:11

TWiStErRob


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.

like image 22
P Vartak Avatar answered Nov 15 '22 12:11

P Vartak


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

like image 3
Shaahin Sh Avatar answered Nov 15 '22 13:11

Shaahin Sh


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

like image 2
Thu Thu Avatar answered Nov 15 '22 12:11

Thu Thu


I had the same issue, after removing the following

android:tag="xx"

from the image on my xml layout, that issue was gone

like image 1
Shai Epstein Avatar answered Nov 15 '22 13:11

Shai Epstein


If you have freedom to use other library, use, else you can use setTag(int, Object), but if you can't do above then wrap ImageView in a basic LinearLayout > setTag() on LinearLayout > setOnClickListener on LinearLayout.

Something like :

LinearLayout imgViewLinearLayout = new LinearLayout(context);
            imgViewLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            imgViewLinearLayout.setId(R.id.dummy_image_layout);
            imgViewLinearLayout.setOnClickListener(this);

ImageView imageView = new ImageView(inflater.getContext());
                imageView.setPadding(0,15,20,0);

                imageView.setId(R.id.imageview_for_glide);

                imgViewLinearLayout.setTag("Image VIEWTAG");
imgViewLinearLayout.addView(imageView)

Override onClick and :

@Override
    public void onClick(View v) {

        if(v.getId() == R.id.dummy_image_layout){
            //Do what you wanna wanted to do on click of ImageView
        }
    }

Hope this helps.

like image 1
Ali Ashraf Avatar answered Nov 15 '22 13:11

Ali Ashraf