android - ListAdapter submitList 未更新

标签 android kotlin android-recyclerview android-diffutils android-listadapter

我在 RecyclerView 中遇到了分页无限滚动的问题,我正在使用 .addAll() 添加所有新项目

 movieList.addAll(it.movieList)
 adapter.submitList(movieList)
 Log.wtf("WTF", movieList.size.toString())
每当我们从 API 获得成功响应时,大小就会不断增加,这表明列表确实正在填充,但 RecyclerView 中的项目保持不变并且 submitList()似乎只在第一次通话时起作用。
这是我的 DiffUtil 类和适配器
class DiffUtilMovies : DiffUtil.ItemCallback<MovieItem>() {

    // DiffUtil uses this test to help discover if an item was added, removed, or moved.
    override fun areItemsTheSame(oldItem: MovieItem, newItem: MovieItem): Boolean {
        return oldItem.id == newItem.id
    }

    // Check whether oldItem and newItem contain the same data; that is, whether they are equal.
    // If there are differences between oldItem and newItem, this code tells DiffUtil that the item has been updated.
    override fun areContentsTheSame(oldItem: MovieItem, newItem: MovieItem): Boolean {
        // Check for now if there is a difference on the price, removing specific fields
        // means checking all the data for changes
        return oldItem.title == newItem.title
    }

}


class MovieAdapter(private val context: Context) : ListAdapter<MovieItem, MovieAdapter.ItemView>(DiffUtilMovies()) {

    private var isDetached: Boolean = false

    class ItemView(itemView: MovieCardBinding) : RecyclerView.ViewHolder(itemView.root) {
        val titleTxt = itemView.titleTxt
        val rateTxt = itemView.rateTxt
        val rateBar = itemView.rateBar
        val imageThumb = itemView.thumbnail
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemView {
        return ItemView(
            MovieCardBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ItemView, position: Int) {
        holder.apply {

            val movieItem = getItem(position)

            titleTxt.text = movieItem.title
            rateTxt.text = movieItem.voteAverage.toString()

            val rateAvg = movieItem.voteAverage?.toFloat() ?: run {
                0.0f
            }

            rateBar.rating = rateAvg/2

            if (!isDetached)
                GlideApp.with(context)
                    .load(context.getString(R.string.image_link,AppConfig.image_endpoint, movieItem.posterPath))
                    .thumbnail(GlideApp.with(context).load(R.drawable.loading).centerCrop())
                    .error(R.drawable.no_image)
                    .into(imageThumb)

            this.itemView.setOnClickListener {

                try {

//                    context.startActivity(Intent(context, AssetInfoActivity::class.java).apply {
//                        putExtra(context.getString(R.string.assets), movieItem)
//                    })

                }
                catch (ignored: Exception){
                    // The user probably already leave before the activity started
                }

            }

        }
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        isDetached = true
    }
}

最佳答案

ListAdapter 不适用于可变列表。这是因为如果您修改 List 的内容,当它尝试比较旧列表和新列表的内容时,它会将同一个列表与自身进行比较。没有旧的列表实例仍然保存旧的内容,因此它无法检测到任何差异。
例如,您应该创建一个新列表,而不是改变原始列表

movieList = movieList + it.movieList
adapter.submitList(movieList)
或者,您可以使用可变后备列表,但在将其传递给 submitList 时始终创建一个副本。 .即使是第一次传递 List 时,您也必须使用副本,因此它永远不会引用您的可变 List。
movieList.addAll(it.movieList)
adapter.submitList(movieList.toList())

关于android - ListAdapter submitList 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70106544/

相关文章:

string - 如何用 kotlin 中第一个字符以外的星号替换字符串

java - 使用 RecyclerView 动态绑定(bind)简单待办事项列表中的列表项

java - 如何在使用表面 View 录制视频时拍照(快照)

android - 应用程序升级后丢失 SQLite 数据库数据 - 怎么会这样?

kotlin - Kotlin Android 10中的Notification中没有自定义声音

带有折叠工具栏的 RecyclerView 中的 Android 字母快速 ScrollView

java - RecyclerView Espresso 测试因 RunTimeException 失败

android - 如何将数据设置为由 fragment 膨胀的布局

android - Android-MenuDrawer 编译错误

android - 了解 PagingSource 的 getRefreshKey