准备显示图像时如何在recyclerview中添加侦听器

时间:2018-12-18 03:33:26

标签: android kotlin android-recyclerview

当前,我有一个使用Picasso下载图像并将其显示在recyclerview中的应用。

我想通过完全取消毕加索并在最初安装该应用程序时在后台下载图像来更改逻辑。在应用程序成功运行期间,图像将从内部存储中显示。

那么,如何将图像成功保存到内部存储器中后让recyclerview知道该图像已准备好显示?我希望如果不一次全部显示它们,因为那会花费很长时间,但是要在下载完成后显示每张图像(有点类似于毕加索的做法)。

这是我当前的onBindViewHolder代码:

 override fun onBindViewHolder(rawHolder: RecyclerView.ViewHolder, position: Int) {
        val holder = rawHolder as CustomVH
        holder.resourcesByCategory = resourcesByCategory[position]

        val dir : String = utils.getInternalDirectory("Covers") + "/CoverBookId" +  resourcesByCategory[position].id + ".jpg"
        val bookCover = File(dir)
        if (bookCover.exists()) {
            if (holder.bmp != null) {
                holder.bmp!!.recycle()
            }

            holder.resourceCover.setImageBitmap(null)
            val options = BitmapFactory.Options()
            options.inPreferredConfig = Bitmap.Config.RGB_565
            holder.bmp = BitmapFactory.decodeStream(FileInputStream(bookCover), null, options)

            if (holder.bmp != null) {
                holder.bmp = Bitmap.createScaledBitmap(holder.bmp, 150,180, true)
                holder.resourceCover.setImageBitmap(holder.bmp!!)
            }

        }

    }

0 个答案:

没有答案