滑行监听器不起作用

Glide listener doesn't work

我正在使用 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 中运行,但如果我删除侦听器并让异步下载没有回调,它会正确运行:

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 中运行.

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

运行 进入同一期。让 onResourceReady return false 对我有用。

这是一种方法:

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

运行 进入同一问题,因为我的 ImageView 的宽度和高度为 0,0(layout_widthlayout_height 都设置为 wrap_content最初 ImageView 上没有设置图像)。给 ImageView 默认宽度和高度解决了我的问题。

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

它适用于我 glide 4.9.1

如果你看看 RequestListener 注释你应该明白。

这对我有用

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

Kotlin 使用 Glide with listener 的方式

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)