滑音监听器不起作用

我正在使用Glide来加载图像,我添加了一个侦听器,以了解资源何时准备就绪,或者是否有任何types的错误:

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但如果我删除监听器,让asynchronous下载没有callback,它会正确运行:

 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而不是侦听器来接收callback,但应用程序在onLoadStarted运行,但永远不会在onLoadClearedonLoadFailedonResourceReady

Solutions Collecting From Web of "滑音监听器不起作用"

如果ImageView不可见或不存在,它似乎是一个Bug。 我在这里打开了一个问题: https : //github.com/bumptech/glide/issues/618

跑到同样的问题。 有onResourceReady返回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()