将参数传递给RequestListener Glide

时间:2016-07-07 13:58:32

标签: android android-recyclerview android-glide

我正在使用Glide(版本3)在带有Recyclerview回调的RequestListener(onBindViewHolder)中加载图片。每当加载图像时,第一次链接将存储在名称为key的SharedPreferences文件中。现在我想将名称传递给RequestListener作为参数。如何将name作为参数传递给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;
        }
    };

1 个答案:

答案 0 :(得分:1)

在这种情况下,创建一个类似乎是合理的,调用它CustomRequestListener并实现RequestListener<String, GlideDrawable> - 向CustomRequestListener添加一个构造函数,它带有String key和{ {1}}。 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;
        }

};

请参阅some good examples of using RequestListener

我希望这会有所帮助。