vihkat
vihkat

Reputation: 1005

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?

Upvotes: 3

Views: 3102

Answers (5)

vinit hanabar
vinit hanabar

Reputation: 1

This will work for sure

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

this code ensure that the loaded image is not stored in memory (skipMemoryCache(true)) or on disk (diskCacheStrategy(DiskCacheStrategy.NONE)), so it's fetched fresh each time from the source.

Upvotes: 0

Timchang Wuyep
Timchang Wuyep

Reputation: 821

Glide.with(context)
        .setDefaultRequestOptions(RequestOptions().frame(1000000L)) // Set the time in microseconds
        .load(videoPath)
        .skipMemoryCache(true)
        .diskCacheStrategy(DiskCacheStrategy.NONE)
        .into(imageView)

Upvotes: 0

Linh
Linh

Reputation: 60923

If your image can come from both local drawable or remote url.
There is a chance that you will get duplicated image. Basically, your local image may be override by remote image.
Then before loading local drawable, you should cancel loading process which happened on this image

You can find the example here 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);
    }
}

Upvotes: 0

DAA
DAA

Reputation: 1386

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

view.main_image.setImageBitmap(null)
Glide.with(...)

Upvotes: 3

mmfarzaneh
mmfarzaneh

Reputation: 397

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

Upvotes: 4

Related Questions