Glide监听器无法工作。

54

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

如果我移除监听器并让异步下载没有回调,那么这个应用程序就不会在 onResourceReadyonException 内运行,但是它可以正确运行:

如果我移除监听器并让异步下载没有回调,那么这个应用程序就可以正确运行,但它不会在 onResourceReadyonException 中运行。
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);

我也尝试使用 GlideDrawableImageViewTarget 替代监听器来接收回调,但应用程序仅在 onLoadStarted 中运行,而从不在 onLoadClearedonLoadFailedonResourceReady 中运行。


你是说监听器的 onException 和 onResourceReady 方法没有被调用?从这些方法中返回 true 将阻止调用 Target,但无论如何都应该始终为监听器调用它们。 - Sam Judd
我认为你需要调用 submit 来启动它开始加载。 - android developer
7个回答

43

6
哎呀,我正在从Picasso转换到Glide,但这太出乎意料了。如果视图仍然不可见,Glide不会调用回调函数 :( - Dr Deo

29

这里有一种方法:

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

2
这很奇怪,添加.submit() 似乎已经在我的端上解决了问题。有任何想法是什么变化导致 .load(...) 不再直接工作?文档对我来说为空。 - Sander van't Veer
我认为你需要在某个时刻启动它,因为如果没有目标(或者只是告诉它去做),它不知道何时执行操作。 - android developer
1
在 .submit() 之后,我无法写入 .into(imageview)。 - karthik kolanji
3
你已经拥有了受众。在 onResourceReady 中实现它。 - android developer
最后,一个 Kotlin 的例子。这是非常需要的。 - Akito

11

遇到了同样的问题。在onResourceReady返回false对我有帮助。


@styler1972,我已经添加了我的代码,如果你想看的话。在我的情况下似乎运行良好。 - android developer

9

您只需要将onResourceReadyonLoadFailed的返回值从true更改为false即可。

这对于我在glide 4.9.1上运行良好。

如果您查看RequestListener的注释,您应该会理解。


6

我遇到了同样的问题,因为我的ImageView的宽度和高度都是0,0(即layout_widthlayout_height都设置为wrap_content,并且初始时没有设置任何图像在ImageView上)。给ImageView设置默认的宽度和高度解决了我的问题。


1
最好提供编码以展示问题是如何解决的。 - Chaska
1
相同的问题是因为imageView的可见性是.GONE - Manuel

0

Kotlin中使用Glide并带有监听器的方法

Glide.with(context)
                .load(image_url)
                .listener(object : com.bumptech.glide.request.RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        img_product_banner.visibility = View.VISIBLE
                        return false
                    }

                }).placeholder(R.drawable.placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(img_product_banner)

-1

这对我有用

try {
        Glide.with(context)
            .asBitmap()
            .load(characterDB.url)
            .listener(object : RequestListener<Bitmap> {
                override fun onResourceReady(
                    resource: Bitmap?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }
            }
            )
            .submit()
    }
    catch (e : Exception)
    {
        Log.d("Excepion",e.message.toString())
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接