错误“您不得在Glide定位的视图上调用setTag()".使用滑翔机时 [英] Error "You must not call setTag() on a view Glide is targeting" when use Glide
问题描述
我在我的应用程序中使用 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"
这部分代码:
@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;
}
那么如何解决呢?
推荐答案
密钥为ViewTarget.setTagId
;设置它将释放ImageView
上的默认setTag
,因此您可以在项目布局中将其用作根目录.
它是在 issue#370 中的Glide 3.6.0中引入的.
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);
}
}
添加以下内容作为src/main/values/ids.xml
的内容:
Add the following as contents for src/main/values/ids.xml
:
<resources>
<item type="id" name="glide_tag" />
</resources>
(或只需将上述<item ... />
添加到values
中的任何resources
xml中)
(or just add the above <item ... />
into any resources
xml in values
)
更新:从4.8.0开始不推荐使用ViewTarget
.如果您使用的是into(ImageView)
,则仍然必须调用不推荐使用的类的方法,因为内置的*ViewTarget
类仍会扩展旧的类.
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.
如果使用自定义的ViewTarget
,请替换为CustomViewTarget
.这将消除设置任何标签ID的需要,因为CustomViewTarget
的默认行为是使用特定于Glide的ID,因此它不应与任何setTag
调用冲突.
如果仍要自定义ID,则可以在自定义目标上使用useTagId
.
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.
这篇关于错误“您不得在Glide定位的视图上调用setTag()".使用滑翔机时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!