Android Jetpack:设置 LiveData 时 RecyclerView 不会更新

Faw*_*ran 5 android kotlin android-recyclerview android-livedata android-jetpack

所以我有一个简单的实现来在 a 中显示用户列表RecyclerView,并在ViewModelas 中查询该列表LiveData

问题是 UI 没有更新以显示最新的列表 - 称为users- 即使观察到该列表。我现在只是设置了一个演示用户列表。

这是我的视图模型:

class MainViewModel : ViewModel() {

    private val demoData = listOf(
            User(userName = "Bob", favoriteColor = "Green"),
            User(userName = "Jim", favoriteColor = "Red"),
            User(userName = "Park", favoriteColor = "Blue"),
            User(userName = "Tom", favoriteColor = "Yellow"),
            User(userName = "Lee", favoriteColor = "Black"),
            User(userName = "Xiu", favoriteColor = "Gray")
    )

    private val _users = MutableLiveData<List<User>>()
    val users: LiveData<List<User>>
        get() = _users

    init {
        _users.value = listOf()
    }

    fun loadUsers() {
        _users.value = demoData.toMutableList().apply { shuffle() }
    }
}
Run Code Online (Sandbox Code Playgroud)

和我的 ViewModel 的附加片段:

// ...

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)

    viewModel.users.observe(this, Observer {
        mAdapter.notifyDataSetChanged()
    })

    mAdapter = UsersAdapter(viewModel.users.value!!)

    mainRV = view?.findViewById<RecyclerView>(R.id.rv_main)?.apply {
        adapter = mAdapter
        layoutManager = LinearLayoutManager(view?.context)
    }

    viewModel.loadUsers()
}
Run Code Online (Sandbox Code Playgroud)

PS 这UsersAdapter是一个平常的RecyclerView.Adapter

我已经确保调用setValue我的用户列表来调用观察者,因此我不确定这里缺少什么。我的适配器设置错误吗?

Epi*_*rce 2

fun loadUsers() {
    _users.value = demoData.toMutableList().apply { shuffle() }
}
Run Code Online (Sandbox Code Playgroud)

toMutableList()使用数据创建一个新列表,请参阅源代码:

public fun <T> Collection<T>.toMutableList(): MutableList<T> {
    return ArrayList(this)
}
Run Code Online (Sandbox Code Playgroud)

因此,您应该更新适配器中的列表并显示它,而不是获取初始值并且从不更新适配器。

viewModel.users.observe(this, Observer { users ->
    mAdapter.updateData(users)
})
Run Code Online (Sandbox Code Playgroud)

如果您不使用ListAdapter,那么您可以像这样定义此方法:

class MyAdapter: RecyclerView.Adapter<ViewHolder>(
   private var list: List<User> = Collections.emptyList()
) {
    ...

    fun updateData(users: List<User>) {
        this.users = users
        notifyDataSetChanged()
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用ListAdapterandsubmitList,您还将获得动画。