错误“您不得在 Glide 所针对的视图上调用 setTag()"使用 Glide 时 [英] 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.
在您的清单中添加:
<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>
(或者只是将上面的
添加到 values
中的任何 resources
xml 中)
(or just add the above <item ... />
into any resources
xml in values
)
更新:ViewTarget
自 4.8.0 起已弃用.如果您使用 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()"使用 Glide 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!