Why Glide blink the item ImageView when notifydatasetchanged

空扰寡人 提交于 2019-12-29 04:19:08

问题


I am using Glide 3.7.0 with RecyclerView. The item view always blinks when refreshing (calling notifyDataSetChanged).

Here is my code:

Glide
  .with(context)
  .load(filepath)
  .diskCacheStrategy(DiskCacheStrategy.NONE)
  .skipMemoryCache(true)
  .dontAnimate()
  .into(imageview);

When I use no cache, the ImageView has a null Bitmap when notifyDataSetChanged method is called and Glide hasn't finished loading the bitmap.

If I use the code below:

Glide
  .with(context)
  .load(filepath)
  .dontAnimate()
  .into(imageview);

Then the item ImageView does not blink anymore (using cache).

I want to update the item view dynamically, so I disable the glide cache.

Are there any solutions to solve this blink bug?

Thank you very much!


回答1:


After my many tries, just use SimpleTarget solved my problem thank you!

Glide
.with(context)
.load(filepath)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.dontAnimate()
.into(new SimpleTarget<Bitmap>() {

            @Override
            public void onResourceReady(Bitmap arg0, GlideAnimation<? super Bitmap> arg1) {
                // TODO Auto-generated method stub
                holder.mItemView.setImageBitmap(arg0);
            }
        });



回答2:


Update Glide from version 3 to 4 and setSupportsChangeAnimations(false) for RecyclerView solved problem for me

RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
    ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}



回答3:


In my case, I solved the issue by using defined dimensions on my imageView.

<ImageView
        android:id="@+id/poster_imageview"
        android:layout_width="130dp"
        android:layout_height="183dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/placeholder" />



回答4:


Also don't forget to setHasStableIds(true); in your adapter and properly override getItemId() method.




回答5:


since SimpleTarget is deprecated try this solution:

GlideApp.with(SOMETHING)
                                .load("WHAT")
                                .dontAnimate()
                                .let { request ->
                                    if(imageView.drawable != null) {
                                        request.placeholder(imageView.drawable.constantState?.newDrawable()?.mutate())
                                    } else {
                                        request
                                    }
                                }
                                .into(imageView)

you can also create nice extension for drawable to make REAL copy:

import android.graphics.drawable.Drawable

fun Drawable.copy() = constantState?.newDrawable()?.mutate()


来源:https://stackoverflow.com/questions/37944860/why-glide-blink-the-item-imageview-when-notifydatasetchanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!