使Glide在所有课程中都作为单身人士工作

时间:2019-04-04 13:58:20

标签: android android-glide

我正在研究负责处理Glide图片加载的模块

所有其他类都必须使用GlideFeatureModule加载图像

GlideFeatureModule

interface IGlideLoad{
    fun load(file: Any):GlideLoadImp
    fun context(context: Fragment):GlideLoadImp
    fun context(context: AppCompatActivity):GlideLoadImp
    fun context(context: Context):GlideLoadImp
    fun placeHolder(drawable: Drawable):GlideLoadImp
    fun placeHolder(drawable: Int):GlideLoadImp
    fun apply(requestOptions: BaseRequestOptions<*>):GlideLoadImp
    fun into(imageView: ImageView):GlideLoadImp
    fun build():GlideLoadImp
}

使用GlideFeatureModule的类

glideLoad
         .context(context)
         .load(url)
          .into(profileImage);

现在,我陷入了如何使用Glide提供的回调

.listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(...) {

                        }

                        @Override
                        public boolean onResourceReady(...) {

                        }
                    })

我想以某种方式使用它

glideLoad.listener(new GlideLoadImp.GlideStatus() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onFailed() {

                        }
                    });

1 个答案:

答案 0 :(得分:1)

您无需创建自定义回调即可从RequestListener界面获得响应。

那么如何获取回调?您只需将接口对象作为方法参数传递,以便可以处理回调。

看下面的例子:

/**
 * Method that loads image explicitly to view passed as parameter provided by string url to be load into.
 * It receives callback as method parameter to handle result.
 * @param view as {@link ImageView} object
 * @param url as {@link String} parameter
 * @param listener as {@link RequestListener} object
 */
public void loadImage(@NonNull ImageView view, String url, RequestListener<Drawable> listener) {
    Glide.with(view.getContext())
            .load(url)
            .listener(listener)
            .into(view);
}

&如何使用

loadImage(imageView, "myUrl", new RequestListener<Drawable>() { //Here we will get callback
        @Override
        public boolean onLoadFailed(@android.support.annotation.Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            return false;
        }
    });
  

编辑收件人:

您可以尝试一下,假设您已经将如下类用作中介者:

class GlideLoad{
    //we store our listener object here to provide callback
    var callback : GlideLoadImp.GlideStatus? = null

    fun listener(listener: GlideLoadImp.GlideStatus?) {
        this.callback = listener
    }
}

,然后使用O.P.中的方法从外部进行设置,

glideLoad.listener(object: GlideLoadImp.GlideStatus() {

        override fun onSuccess() {

        }

        override fun onFailed() {

        }
})

现在在RequestListener回调期间,我们可以提供值或调用回调侦听器,如下所示:

class GlideLoad {
... Some code
// some method having our RequestListener implementation
    object: RequestListener<Drawable>() {

        override fun Boolean onLoadFailed(...) {
            callback?.onFailed(you can pass params here if needed)
        }

        override fun Boolean onResourceReady(...) {
            callback?.onSuccess(you can pass params here if needed)
        }
    }
... Some other code
}