Glide听众不起作用

Gio*_*oli 35 android callback imageview android-glide

我正在使用Glide加载图像,我添加了一个监听器,以了解资源何时准备好或是否存在任何类型的错误:

Glide.with(mContext)
    .load(url)
    .placeholder(R.drawable.glide_placeholder)
    // use dontAnimate and not crossFade to avoid a bug with custom views
    .dontAnimate()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            // do something
            return true;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            // do something
            return true;
         }
    })
    .into(mCustomImageView);
Run Code Online (Sandbox Code Playgroud)

该应用程序永远不会在内部运行onResourceReady,onException但如果我删除了侦听器并让异步下载没有回调,它运行正常:

Glide.with(mContext)
    .load(url)
    .placeholder(R.drawable.glide_placeholder)
    // use dontAnimate and not crossFade to avoid a bug with custom views
    .dontAnimate()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .into(mCustomImageView);
Run Code Online (Sandbox Code Playgroud)

我也尝试使用GlideDrawableImageViewTarget而不是监听器来接收回调但是应用程序在内部运行onLoadStarted但从未在内部运行onLoadCleared,onLoadFailed并且onResourceReady.

Gio*_*oli 31

如果ImageView的可见性不见或消失,它似乎是一个错误.我在这里打开了一个问题:https://github.com/bumptech/glide/issues/618

  • 天哪,我正在从毕加索过渡到 Glide,但这出乎意料。如果视图仍然不可见,Glide 不会调用回调:( (4认同)

and*_*per 15

这是一种方法:

        Glide.with(context).load(...)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                        //TODO handle error images while loading photo
                        return true
                    }

                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        //TODO use "resource" as the photo for your ImageView
                        return true
                    }

                }).submit()
Run Code Online (Sandbox Code Playgroud)

  • 嗯,这很奇怪,添加 .submit() 似乎解决了我的问题。知道进行了哪些更改导致 .load(...) 不再直接工作吗?文档对我来说是空白的。 (2认同)
  • @karthikkolanji 你有听众。在“onResourceReady”中进行 (2认同)

小智 8

您只需要将onResourceReady和的返回值onLoadFailed从 true更改为 false。

它对我有用glide 4.9.1

如果您查看 RequestListener 评论,您应该明白。


m3g*_*r0n 6

遇到了同样的问题,因为我的宽度和高度ImageView分别为0,0(两者layout_widthlayout_height分别设置为wrap_content无图像设定于ImageView最初)。给出ImageView默认宽度和高度解决了我的问题。


Rik*_*zen 5

遇到同样的问题。使onResourceReady返回false对我有用。