Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使Glide在所有课程中都能作为单子工作_Android_Android Glide - Fatal编程技术网

Android 使Glide在所有课程中都能作为单子工作

Android 使Glide在所有课程中都能作为单子工作,android,android-glide,Android,Android Glide,我正在处理模块,该模块负责处理Glide图像加载 所有其他类都必须使用GlideFeatureModule加载图像 GlideFeatureModule interface IGlideLoad{ fun load(file: Any):GlideLoadImp fun context(context: Fragment):GlideLoadImp fun context(context: AppCompatActivity):GlideLoadImp fun c

我正在处理模块,该模块负责处理
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
}
glideLoad
         .context(context)
         .load(url)
          .into(profileImage);
现在我对如何使用
Glide
提供的回调感到困惑

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

                        }

                        @Override
                        public boolean onResourceReady(...) {

                        }
                    })

您不需要创建自定义回调来从
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);
}
我们用从O.p.开始的方法从外部设置

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

        override fun onSuccess() {

        }

        override fun onFailed() {

        }
})
现在,在我们的
RequestListener
回调过程中,我们可以提供值或调用我们的回调侦听器,如下所示:

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

    fun listener(listener: GlideLoadImp.GlideStatus?) {
        this.callback = listener
    }
}
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
}
类负载{
…一些代码
//某些方法具有我们的RequestListener实现
对象:RequestListener(){
重写onLoadFailed(…){
回调?.onFailed(如果需要,可以在此处传递参数)
}
重写onResourceReady(…){
回调?.onSuccess(如果需要,可以在此处传递参数)
}
}
…其他代码
}

您可以为此创建扩展功能,如果您不知道,我将提供。请提供我。。。这将非常有帮助。您能详细解释一下您想从这个函数中实现什么吗?只是想隐藏加载程序并使imageview在onSuccess()中可见,谢谢您的努力。这将使我的代码直接依赖于Glide库,这是我不想要的。我需要单独创建Glide,以便以后可以将其更改为任何其他库。在这种情况下,您想实现回调接口吗?是的。。。这就是我要找的。我实现了它,但glide中的RequestOption不能接受null对象,因此尝试创建一个扩展函数:)
glideLoad.listener(object: GlideLoadImp.GlideStatus() {

        override fun onSuccess() {

        }

        override fun onFailed() {

        }
})
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
}