Glide listener doesn't work

2019-01-26 03:27发布

I'm using Glide to load images and I added a listener to know when resource is ready or if there was an error of any type:

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

The app never runs inside onResourceReady or onException but if I remove the listener and let the async download without a callback, it runs correctly:

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

I tried also with GlideDrawableImageViewTarget instead of listener to receive callbacks but app runs inside onLoadStarted but never runs inside onLoadCleared, onLoadFailed and onResourceReady.

4条回答
小情绪 Triste *
2楼-- · 2019-01-26 03:27

Here's one way to do it:

        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()
查看更多
唯我独甜
3楼-- · 2019-01-26 03:39

Ran into same issue. Having onResourceReady return false did the trick for me.

查看更多
神经病院院长
4楼-- · 2019-01-26 03:51

Ran into same issue ,because the width and height of my ImageView were 0,0. Gave my ImageView a default width and Height this solved my issue.

查看更多
姐就是有狂的资本
5楼-- · 2019-01-26 03:52

It seems to be a bug with ImageView's visibility if it's invisible or gone. I opened an issue here: https://github.com/bumptech/glide/issues/618

查看更多
登录 后发表回答