ListAdapter 不更新 RecyclerView 中的项目 [英] ListAdapter not updating item in RecyclerView

查看:45
本文介绍了ListAdapter 不更新 RecyclerView 中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的支持库 ListAdapter.这是我的适配器代码

I'm using the new support library ListAdapter. Here's my code for the adapter

class ArtistsAdapter : ListAdapter<Artist, ArtistsAdapter.ViewHolder>(ArtistsDiff()) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(parent.inflate(R.layout.item_artist))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        fun bind(artist: Artist) {
            itemView.artistDetails.text = artist.artistAlbums
                    .plus(" Albums")
                    .plus(" u2022 ")
                    .plus(artist.artistTracks)
                    .plus(" Tracks")
            itemView.artistName.text = artist.artistCover
            itemView.artistCoverImage.loadURL(artist.artistCover)
        }
    }
}

我正在用

musicViewModel.getAllArtists().observe(this, Observer {
            it?.let {
                artistAdapter.submitList(it)
            }
        })

我的差异类

class ArtistsDiff : DiffUtil.ItemCallback<Artist>() {
    override fun areItemsTheSame(oldItem: Artist?, newItem: Artist?): Boolean {
        return oldItem?.artistId == newItem?.artistId
    }

    override fun areContentsTheSame(oldItem: Artist?, newItem: Artist?): Boolean {
        return oldItem == newItem
    }
}

发生的情况是,当适配器第一次渲染所有项目时调用 submitList,但是当使用更新的对象属性再次调用 submitList 时,它不会重新渲染已更改的视图.

What's happening is when submitList is called the first time the adapter renders all the items, but when submitList is called again with updated object properties it does not re-render the view which has changed.

它在我滚动列表时重新渲染视图,然后调用 bindView()

It re-renders the view as I scroll the list, which in turn calls bindView()

另外,我注意到在提交列表后调用 adapter.notifyDatasSetChanged() 会使用更新的值呈现视图,但我不想调用 notifyDataSetChanged() 因为列表适配器内置了 diff utils

Also, I've noticed that calling adapter.notifyDatasSetChanged() after submit list renders the view with updated values, but I don't want to call notifyDataSetChanged() because the list adapter has diff utils built-in

有人可以帮我吗?

推荐答案

我明白为什么会发生这种情况,这不是我的观点.我的观点是它至少需要发出警告或调用 notifyDataSetChanged() 函数.因为显然我是出于某种原因调用 submitList(...) 函数.我很确定人们在几个小时内都在试图弄清楚出了什么问题,直到他们发现 submitList() 默默地忽略了调用.

I understand why this happens that wasn't my point. My point is that it at least needs to give a warning or call the notifyDataSetChanged() function. Because apparently I am calling the submitList(...) function for a reason. I am pretty sure people are trying to figure out what went wrong for hours until they figure out the submitList() ignores silently the call.

这是因为 Google 的奇怪逻辑.因此,如果您将相同的列表传递给适配器,它甚至不会调用 DiffUtil.

This is because of Googles weird logic. So if you pass the same list to the adapter it does not even call the DiffUtil.

public void submitList(final List<T> newList) {
    if (newList == mList) {
        // nothing to do
        return;
    }
....
}

我真的不明白这个 ListAdapter 的全部意义,如果它不能处理同一个列表上的变化.如果您想更改传递给 ListAdapter 的列表中的项目并查看更改,那么您需要创建列表的深层副本或需要使用常规的 RecyclerView 与您自己的 DiffUtill 类.

I really don't understand the whole point of this ListAdapter if it can't handle changes on the same list. If you want to change the items on the list you pass to the ListAdapter and see the changes then either you need to create a deep copy of the list or you need to use regular RecyclerView with your own DiffUtill class.

这篇关于ListAdapter 不更新 RecyclerView 中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆