我正在使用Glide
(第3版)通过Recyclerview
回调在RequestListener
(onBindViewHolder)中加载图像。每当首次加载图像时,链接将存储在名称为key
的SharedPreferences文件中。现在,我想将名称作为参数传递给RequestListener
。如何将名称作为参数传递给RequestListener?而且我还必须传递ImageView,以便在加载失败的情况下可以在onException方法中加载。
码:
onBindViewHolder方法:
@Override
public void onBindViewHolder(final ContactsAdapter.ContactsViewHolder holder, int position) {
final ContactInfo current = cDataset.get(position);
holder.contactName.setText(current.Contact_name);
//current.Conatct_name has to be passed to requestListener
URL url = Util.getSignedUrl(getContext().getApplicationContext(), current.phone_number.concat("/").concat("profile").concat("/").concat(current.phone_number).concat(".jpg"));
if (url != null) {
Log.d("image", url.toString());
Glide.with(getContext()).load(url.toString()).skipMemoryCache(true).fitCenter().diskCacheStrategy(DiskCacheStrategy.SOURCE).listener(requestListener).error(R.mipmap.ic_launcher).into(holder.contactProfileImage);
} else {
Log.d("image", "null");
}
}
RequestListener:
public RequestListener<String, GlideDrawable> requestListener = new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
Log.d("No Image",String.valueOf(e));
Log.d("Loading from cache","true");
return true;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
Log.d("storing cache ","true");
return false;
}
};
最佳答案
在这种情况下,创建一个类,将其命名为CustomRequestListener
并实现RequestListener<String, GlideDrawable>
-向CustomRequestListener
添加一个构造器,该构造器采用String key
和ImageView
似乎是合理的。 CustomRequestListener
甚至可以是一个内部类。这是一个代码示例:
private class CustomRequestListener implements RequestListener<String, GlideDrawable>{
//variables to hold the arguments you will be passing to the constructor
private String key;
private ImageView imageView;
//constructor taking the arguments as you desire
public CustomRequestListener(String _key, ImageView defaultImageView){
this.key = _key;
this.imageView = defaultImageView
}
//the rest of your code goes here
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
Log.d("No Image",String.valueOf(e));
Log.d("Loading from cache","true");
//you can now load the "default" imageView here...
return true;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
Log.d("storing cache ","true");
//here you can save the URL into the SharedPreference - using the "key" variable as its key. Example:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefs.edit();
editor.putString(key, theURL);
editor.apply();
return false;
}
};
然后,使用自定义侦听器,您需要执行以下操作:
Glide.with(getContext()).load(url.toString()).skipMemoryCache(true).fitCenter().diskCacheStrategy(DiskCacheStrategy.SOURCE).listener(new CustomRequestListener (someKey, someImageView)....
见some good examples of using RequestListener
我希望这有帮助。