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

查看:822
本文介绍了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

有人可以在这里帮助我吗?

Can anyone help me here?

推荐答案

我理解为什么这不是我的意思.我的观点是,它至少需要发出警告或调用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并查看更改,然后要么创建列表的深层副本,要么需要对自己的DiffUtill类使用常规的RecyclerView.

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天全站免登陆