为什么 Glide 只在第二次加载图像?(Android)

3

我正在使用适配器来显示个人资料图标列表。我正在使用Glide来显示这些图片。但问题是,它加载默认的个人资料图片而不是第一次从响应中加载URL。

当我返回上一页并再次进入该页面时,它才会加载URL。

代码:

@Override
public void onBindViewHolder(RetailerStatusViewHolder holder, int position) {

    if(retailerList.getImgUrl()!=null){
      Glide.with(context) 
            .load(retailerList.getImgUrl())
            .placeholder(R.drawable.ic_default_profile)
            .error(R.drawable.ic_default_profile)
            .into(((holder.circleImageView)));
    }
}

我试过使用context.getApplicationContext(),但图片仍只在第二次加载时显示。

Gradle:

compile 'com.github.bumptech.glide:glide:3.8.0'

XML:

<com.customviews.CircleImageView
    android:id="@+id/recycle_profile"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_gravity="center"
    android:layout_marginLeft="16dp"
    android:src="@drawable/ic_default_profile"/>

Glide 从 URL 加载图片需要一些时间。 - AskNilesh
1
Glide 加载图像需要一些时间,而且在加载圆形 ImageView 时也会出现问题。请参阅此链接,它将帮助您找到问题所在。 - Farhana Naaz Ansari
1个回答

0

试试这个:

XML 代码:

<set xmlns:android="http://schemas.android.com/apk/res/android">

<alpha
    android:duration="800"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toAlpha="1.0"/>

Java类:

public static void loadImage(final Activity context, ImageView imageView, String url, int placeHolderUrl, int errorImageUrl) {
    if (context == null || context.isDestroyed()) return;

    //placeHolderUrl=R.drawable.ic_user;
    //errorImageUrl=R.drawable.ic_error;
    Glide.with(context) //passing context 
            .load(getFullUrl(url)) //passing your url to load image.
            .placeholder(placeHolderUrl) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
            .error(errorImageUrl)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
            .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
            .animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
            .fitCenter()//this method help to fit image into center of your ImageView 
            .into(imageView); //pass imageView reference to appear the image.
}

在你的活动中添加这行代码:

Utils.loadImage(YourClassName.this,mImageView,url,R.drawable.ic_user,R.drawable.ic_error);

在片段中:

Utils.loadImage(getActivity,mImageView,url,R.drawable.ic_user,R.drawable.ic_error);

我觉得它会对你有帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接