问题描述
我有这个问题 - 只是为了测试目的,我将 ParseFile
添加到接收列表中的 ParseObject
之一.它不是仅在该行中显示,而是每 4-5 行显示一次,有时更多,有时更少.我怀疑回收观点与此有关.奇怪的是,其他数据(从本例中删除)与 position
变量一起工作正常.
I have this problem - just for testing purposes I added ParseFile
to one of ParseObject
from received list. Instead of showing it only in that row it shows every 4-5 rows, sometimes more, sometimes less. I supspect that recycling view have something to do with this. Strangely, other data (deleted from this example) works fine with position
variable.
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(parseList.get(position).get("logo") != null){
ParseFile image = (ParseFile) parseList.get(position).get("logo");
String url = image.getUrl();
Glide.with(context)
.load(url)
.placeholder(R.drawable.piwo_48)
.transform(new CircleTransform(context))
.into(holder.imageView);
}
}
推荐答案
这里的答案是不正确的,尽管它们在正确的轨道上.
The answers here are incorrect, although they're on the right track.
你需要调用Glide#clear()
,而不是仅仅将图像drawable设置为null.如果不调用 clear()
,异步加载无序完成仍可能导致视图回收问题.您的代码应如下所示:
You need to call Glide#clear()
, not just set the image drawable to null. If you don't call clear()
, an async load completing out of order may still cause view recycling issues. Your code should look like this:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (parseList.get(position).get("logo") != null) {
ParseFile image = (ParseFile) parseList.get(position).get("logo");
String url = image.getUrl();
Glide.with(context)
.load(url)
.placeholder(R.drawable.piwo_48)
.transform(new CircleTransform(context))
.into(holder.imageView);
} else {
// make sure Glide doesn't load anything into this view until told otherwise
Glide.with(context).clear(holder.imageView);
// remove the placeholder (optional); read comments below
holder.imageView.setImageDrawable(null);
}
}
这篇关于Recyclerview Adapter 和 Glide - 每 4-5 行相同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!