ImageView RecyclerView 延迟加载

ImageView RecyclerView Lazy Load

我在 onBindViewHolder 中有一个 RecyclerViewAdapter,我调用 AsyncTask 从 Google Cloud Storage 下载所需的图像。

RecyclerViewAdapter 中有一些项目需要相同的图像。我试图通过只下载本地文件存储中不存在的图像来解决这个问题;否则,如果确实如此,则它会使用已经下载的图像。

现在发生的事情是,如果 RecyclerView 中有多个项目在足够接近的同一时间调用同一图像,其中一个 ImageView 项目将只是 blank/white,但其余项目将显示图片正确。

我希望能够捕捉到这种情况,只需使用 Thread.sleep,然后再次尝试 get/use 文件,但我没有收到任何异常抛出或任何东西,所以我甚至不确定如何捕获该场景来处理它。

我在 logcat 中看到的唯一有用的信息如下:

D/skia: --- Failed to create image decoder with message 'unimplemented'

但是它并没有抛出异常,它只是将其记录为一条消息。

然而,它确实表明图像解码存在问题。所以我检查了 BitmapFactory.decodeFile 文档,它说如果无法解码它 returns 为 null。因此,我尝试在尝试解码后检查我的位图是否为空,但它从未遇到该 IF 语句,这意味着它永远不会返回空,所以我不确定该怎么做。

new Getlogos(holder.logoImageView.getTag().toString(), holder.itemView.getContext(), new Getlogos.AsyncResponse() {
    @Override
    public void returnBitmapURI(String URI, String tag) {
        if (holder.logoImageView != null && holder.logoImageView.getTag().toString().equals(tag)) {
            if (URI.contains("drawable://")) {
                int id = Integer.parseInt(URI.substring(11));
                Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), id);
                holder.logoImageView.setImageBitmap(myBitmap);
            } else {
                Bitmap myBitmap = BitmapFactory.decodeFile(URI);
                holder.logoImageView.setImageBitmap(myBitmap);
            }
        }
    }
}).execute();

这个问题只是偶尔出现,不会一直出现在同一张图片上。它通常在某些与 download/use 具有相同图像的项目之间有所不同。有时它根本不会发生。

任何人都可以阐明 skia 消息的来源以及解码失败时如何捕获它,以便我处理它吗?

Use Glide to display image in recyclerView

Add this dependencies to build.gradle

implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

在 onBindViewHolder 里面

Request option to show error image, placeholder image etc. CircularProgressDrawable for placeholder drawable(optional)

val circularProgressDrawable = CircularProgressDrawable(mContext)
        circularProgressDrawable.strokeWidth = 5f
        circularProgressDrawable.centerRadius = 30f
        circularProgressDrawable.start()

        val requestOption : RequestOptions = RequestOptions()
                .placeholder(circularProgressDrawable)
                .error(R.drawable.ic_error_1)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .priority(Priority.HIGH)
                .dontAnimate()
                .dontTransform()

initialize Glide as shown below

Glide.with(mContext)
            .load(model.imageUrl)
            .apply(requestOption)
            .into(holder.imageView!!)

如果是这样发生的:

Failed to create image decoder with message 'unimplemented'

你通过判断 "bitmap == null?" 来捕获异常 喜欢以下代码:

我的英文不是很好,希望你能看懂我说的!