Recyclerview适配器和滑翔 - 每4-5行相同的图像 [英] Recyclerview Adapter and Glide - same image every 4-5 rows
问题描述
我有这个问题 - 只是出于测试目的,我添加 ParseFile
到的parseObject
从接收到的列表中的一个。相反,仅表示该行它它显示每行4-5,有时多,有时少。我supspect再循环观点有一些做与此有关。奇怪的是,其他数据(从这个例子中删除)正常工作与位置
变量。
@覆盖
公共无效onBindViewHolder(ViewHolder持有人,INT位置){
如果(parseList.get(位置)获得(标识)!= NULL){
ParseFile图像=(ParseFile)parseList.get(位置)获得(标识);
字符串URL = image.getUrl();
Glide.with(上下文)
.load(URL)
.placeholder(R.drawable.piwo_48)
.transform(新CircleTransform(上下文))
.into(holder.imageView);
} }
这里的答案是不正确的,虽然他们是在正确的轨道上。
您需要调用滑翔#清()
,不只是设置图像绘制为null。如果你不叫清()
,异步加载完成无序仍可能导致视图回收事宜。您code应该是这样的:
@覆盖
公共无效onBindViewHolder(ViewHolder持有人,INT位置){
如果(parseList.get(位置)获得(标识)!= NULL){
ParseFile图像=(ParseFile)parseList.get(位置)获得(标识);
字符串URL = image.getUrl();
Glide.with(上下文)
.load(URL)
.placeholder(R.drawable.piwo_48)
.transform(新CircleTransform(上下文))
.into(holder.imageView);
}其他{
//请确保脚垫不会任何东西,直到另行告知装入这个观点
Glide.clear(holder.imageView);
//删除占位符(可选);阅读下面的评论
holder.imageView.setImageDrawable(NULL);
}
}
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.
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.clear(holder.imageView);
// remove the placeholder (optional); read comments below
holder.imageView.setImageDrawable(null);
}
}
这篇关于Recyclerview适配器和滑翔 - 每4-5行相同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!