Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide recyclerview loading duplicate image

I have a recyclerview with diffutil. Already I using Glide to load images inside the ImageViews.

on the onBindViewHolder I call my function it's called loadImage(holder.view,item)

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val item = getItem(position)

    onLoadImage(holder.view, item)
}

In my loadImage I load the image inside the view.

 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)
    }

It works good, but when first time when It's loading the image than I swipe in the list, and the Images are shows like this:

enter image description here

So the Images are duplicated, but the last two image is different. It happens only if I swipe fast when It's loading. Log:

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

This is a Custom View. Context is that's view's context.

So the Images are different. What is the problem?

Any suggestion?

like image 747
vihkat Avatar asked Apr 01 '26 14:04

vihkat


2 Answers

I know its late but hope it will help someone. Override these two methods in your adapter.

  @Override
public long getItemId(int position) {
  return position;
}

  @Override
public int getItemViewType(int position) {
 return position;
}
like image 106
mmfarzaneh Avatar answered Apr 04 '26 06:04

mmfarzaneh


Try clearing the image before loading a new one in your loadImage method:

view.main_image.setImageBitmap(null)
Glide.with(...)
like image 30
DAA Avatar answered Apr 04 '26 05:04

DAA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!