2016-07-07 43 views
0

我正在使用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; 
     } 
    }; 

回答

1

在這種情況下,它似乎是合理的創建一個類,稱之爲CustomRequestListener它實現RequestListener<String, GlideDrawable> - 添加構造函數爲CustomRequestListener,其中需要String keyImageViewCustomRequestListener甚至可以是一個內部類。下面是一個代碼示例:

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

我希望這有助於。