0

如何解决滚动后发生变化并显示错误图像的子适配器图像?

这是我的代码

父数据类

data class Review(
   val author: String,
   val date: String,
   val rating: Float,
   val comment: String,
   val images: List<Image>)

子数据类

对于作者和评论,我只取自父数据类

data class Image(
   var author: String? = null,
   var comment: String? = null,
   val large: String,
   val thumbnail: String)

父适配器

class ReviewAdapter(private val callback: ProductReviewImageAdapterCallback) :
PagingDataAdapter<Review, ReviewAdapter.ListViewHolder>(DIFF_CALLBACK) {

private val viewPool = RecyclerView.RecycledViewPool()

private val imageAdapter: ProductReviewImageAdapter by lazy {
    ProductReviewImageAdapter(callback)
}

companion object {
    private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Review>() {
        override fun areItemsTheSame(oldItem: Review, newItem: Review): Boolean {
            return oldItem.author == newItem.author
        }

        override fun areContentsTheSame(oldItem: Review, newItem: Review): Boolean {
            return oldItem.comment == newItem.comment
        }
    }
}


inner class ListViewHolder(itemBinding: ItemProductReviewBinding) :
    RecyclerView.ViewHolder(itemBinding.root) {
    val binding = ItemProductReviewBinding.bind(itemBinding.root)
    fun bind(data: Review, position: Int) {
      with(binding) {
            if (data.images.isEmpty()) {
                rvImageReview.gone()
            } else {
                setupImagesRecyclerView(rvImageReview)
                rvImageReview.visible()
                imageAdapter.differ.submitList(data.images)
            }

            tvNameReviewer.text = data.author
            tvReviewDesc.text = data.comment
            tvDateReview.text = data.date
            ratingBarReview.rating = data.rating

        }
    }
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder =
    ListViewHolder(
        ItemProductReviewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    )

override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
    val review = getItem(position)

    if (review != null) {
        holder.bind(review, position)
    }
}

private fun setupImagesRecyclerView(recyclerView: RecyclerView) {
    recyclerView.apply {
        layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        adapter = imageAdapter
        setRecycledViewPool(viewPool)
        setItemViewCacheSize(20)
    }
}

}

子适配器

class ProductReviewImageAdapter(private val callback: ProductReviewImageAdapterCallback) : RecyclerView.Adapter<ProductReviewImageAdapter.ListViewHolder>() {
private val diffCallback = object : DiffUtil.ItemCallback<Image>() {
    override fun areItemsTheSame(
        oldItem: Image,
        newItem: Image
    ): Boolean {
        return oldItem.author == newItem.author
    }

    override fun areContentsTheSame(
        oldItem: Image,
        newItem: Image
    ): Boolean {
        return oldItem.comment == newItem.comment
    }

}

val differ = AsyncListDiffer(this, diffCallback)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder =
    ListViewHolder(
        ItemImageBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    )

override fun getItemCount(): Int = differ.currentList.size

override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
    holder.bind(differ.currentList[position], position)
}

inner class ListViewHolder(itemBinding: ItemImageBinding) :
    RecyclerView.ViewHolder(itemBinding.root) {
    private val itemBinding = ItemImageBinding.bind(itemBinding.root)
    fun bind(data: Image, position: Int) {
        with(itemBinding) {

            image.loadImageRoundedCorner(data.thumbnail)

            image.setOnClickListener {
                callback.onProductReviewImageClicked(position, differ.currentList)
            }

        }

    }
}

}

滚动前

首次加载时,第一个位置数据会显示相应的图像

滚动前

稍微滚动后

第一个位置数据中的图像更改为下一个数据中的图像

稍微滚动后

滚动到最后一个数据/页面

最后一张图片显示了不合适的图片

滚动到最后一个数据/页面

*笔记

  • 只有5个数据,只有第一个和最后一个数据包含图像
  • 第一个位置数据中的图像应该是人物图像,最后一个位置图像应该是粉红色图像
  • 我使用Coil作为图像加载器,并在加载图像之前使用了 ImageView.clear() 方法

提前谢谢,如果我的英语不好,对不起。我不是母语人士

4

1 回答 1

0

更新

修复:原来只是在适配器初始化的时候出错,适配器应该在onBindViewHolder中初始化,而不是在父适配器中,所以不是所有项目都具有相同的适配器,导致所有数据发生变化

于 2020-09-25T19:36:22.207 回答