Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

android - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...