【发布时间】:2020-05-29 04:58:10
【问题描述】:
我想在 Firestore 中发生数据更改时更新我的 Recyclerview 项目。 所以我的活动有以下代码:
taskViewModel.fetchedTaskLiveData.observe(
this, Observer {
if (it != null) {
todoListAdapter.setListData(it)
showRecyclerView()
}
内部适配器setListData方法:
fun setListData(data: MutableList<Todo>) {
//this.todoList.clear()
this.todoList.addAll(data)
notifyDataSetChanged()
}
在这种情况下,每当获取更改的列表时,Recyclerview 都会复制并添加更改的列表 在旧列表下方列出。
如果“this.todoList.clear()”未注释,则列表将被清除,并且数据更改时不会显示任何数据。
我尝试了所有可能的解决方案,但我认为某处缺少一部分。卡了很久。 非常感谢各种建议。
【问题讨论】:
-
我推荐
this.todoList = ArrayList(data),然后是notifyDataSetChanged() -
@EpicPandaForce 试过了。但是没用
-
可能您的 RecyclerView 高度不正确,例如
wrap_content和setHasFixedSize(true) -
是的 @EpicPandaForce 它目前是 height="wrap_content" 和 recyclerview.setHasFixedSize(true)。我需要改变吗?
-
是的。让它
setHasFixedSize(false)。
标签: android mvvm android-livedata mutablelivedata