我有一个
recyclerview
和 diffutil
。我已经使用 Glide
在 ImageViews
中加载图像。
在
onBindViewHolder
上我称我的函数为 loadImage(holder.view,item)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
onLoadImage(holder.view, item)
}
在我的 loadImage 中,我将图像加载到视图中。
private fun loadImage(view: View, item: MyItemModel) {
Timber.i("load item's image id: ${item.id} image is: ${item.image}")
Glide.with(context)
.asDrawable()
.load(item.image)
.into(view.main_image)
}
它工作得很好,但是当第一次加载图像时,我在列表中滑动,图像显示如下:
因此图像是重复的,但最后两张图像不同。仅当我在加载时快速滑动时才会发生这种情况。 日志:
I/MyListAdapter: load image into : 6 image is: [B@25d0674
I/MyListAdapter: load image into : 7 image is: [B@e64ced4
I/MyListAdapter: load image into : 8 image is: [B@b384734
这是自定义视图。上下文是视图的上下文。
所以图像是不同的。 问题是什么?
有什么建议吗?
我知道已经晚了,但希望它能帮助别人。在您的适配器中覆盖这两个方法。
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
在您的
loadImage
方法中加载新图像之前,请尝试清除图像:
view.main_image.setImageBitmap(null)
Glide.with(...)
如果您的图像可以来自本地可绘制或远程 url。
您有可能会得到重复的图像。基本上,您的本地图像可能会被远程图像覆盖。
然后在加载本地可绘制对象之前,您应该取消该图像上发生的加载过程
您可以在这里找到示例 https://bumptech.github.io/glide/doc/getting-started.html
public void onBindViewHolder(ViewHolder holder, int position) {
if (isLoadingRemoteImage) {
...
} else {
Glide.with(context).clear(holder.imageView);
holder.imageView.setImageDrawable(specialDrawable);
}
}
Glide.with(context)
.setDefaultRequestOptions(RequestOptions().frame(1000000L)) // Set the time in microseconds
.load(videoPath)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(imageView)
这肯定有效
override fun onBindViewHolder(holder: CategoriesViewHolder, position: Int){
Glide.with(context)
**.setDefaultRequestOptions(RequestOptions().frame(1000000L)) //add
this line only if you are using gif**
.load(imgList[position])
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(holder.imageview)
//Glide.with(holder.itemView).load(imgList[position]).into(holder.imageview)
}
此代码确保加载的图像不会存储在内存中 (skipMemoryCache(true)) 或磁盘上 (diskCacheStrategy(DiskCacheStrategy.NONE)),因此每次都会从源中获取新鲜的图像。