我正在使用Glide
通過RequestListener
回調在Recyclerview
(onBindViewHolder)中加載圖像。每當圖像首次加載時,鏈接將存儲在名稱爲key
的SharedPreferences文件中。現在我想把這個名字作爲參數傳入RequestListener
。如何將名稱作爲參數傳遞給RequestListener?而且我還必須通過ImageView,以便在加載失敗的情況下加載onException方法。將參數傳遞給RequestListener Glide
代碼:
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;
}
};