在android recyclelerview-Java中给出不同URL的glide加载相同的图像

时间:2018-06-09 13:20:23

标签: android-recyclerview android-glide

我已经很好地实现了滑行,但问题是它为urls中不同recyclerview的不同图像视图加载了相同的图像。

主要问题如何加载所需的图片并仍保留本地缓存以保存用户数据?这是我的代码:

    Glide.with(context).load(objects.get(position)).apply(new RequestOptions().fitCenter().error(R.drawable.logo).placeholder(R.drawable.logo).diskCacheStrategy(DiskCacheStrategy.ALL)).into(new SimpleTarget<Drawable>() {
  @Override
  public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
    imageView.setImageDrawable(resource);
    progressBar.setVisibility(View.GONE);
  }
});

1 个答案:

答案 0 :(得分:1)

代替做这样的事情:

summary(df_list$galactose)
#       chr       location          gene              sample1     sample2       sugar          
#  Min.   :1   Min.   :124353   Length:1           Min.   :1   Min.   :0.5   Length:1          
#  1st Qu.:1   1st Qu.:124353   Class :character   1st Qu.:1   1st Qu.:0.5   Class :character  
#  Median :1   Median :124353   Mode  :character   Median :1   Median :0.5   Mode  :character  
#  Mean   :1   Mean   :124353                      Mean   :1   Mean   :0.5                     
#  3rd Qu.:1   3rd Qu.:124353                      3rd Qu.:1   3rd Qu.:0.5                     
#  Max.   :1   Max.   :124353                      Max.   :1   Max.   :0.5            

str(df_list$glucose)
# 'data.frame': 3 obs. of  6 variables:
#  $ chr     : int  2 3 7
#  $ location: int  12353 23456 657864
#  $ gene    : chr  "ALMS1" "TNN" "MYBC3"
#  $ sample1 : num  2 0 0.3
#  $ sample2 : num  0.1 0 1
#  $ sugar   : chr  "glucose" "glucose" "glucose"

head(df_list$sucrose)
#   chr location gene sample1 sample2   sugar
# 1   1    12345 FAM1     0.1       0 sucrose

只需将.into(new SimpleTarget<Drawable>() { @Override public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) { imageView.setImageDrawable(resource); progressBar.setVisibility(View.GONE); } }); 放入into函数中即可。如果仍然希望在加载完成后得到通知,则可以使用imageView函数。您的代码应如下所示:

listener

Glide应该自己处理RecyclerView生命周期。您唯一需要做的就是隐藏进度条。您无需自己将可绘制对象设置到ImageView中。

更新: 提到的方法是最好的,但是在Glide .with(context) .load(objects.get(position)) .apply(new RequestOptions() .fitCenter() .error(R.drawable.logo) .placeholder(R.drawable.logo) .diskCacheStrategy(DiskCacheStrategy.ALL) ) .listener(new SimpleTarget<Drawable>() { @Override public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) { progressBar.setVisibility(View.GONE); } }) .into(imageView); 中使用lint时会给我带来一些SimpleTarget<?>()错误,这可能是由于版本差异所致,并迫使我使用.listener()和它有效:

RequestListener<Drawable>()