Android Jetpack:设置 LiveData 后 RecyclerView 未更新

Android Jetpack: RecyclerView is not updating when LiveData is set

所以我有一个简单的实现来显示 RecyclerView 中的用户列表,并在 ViewModel 中查询列表作为 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() }
    }
}

我的 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()
}

P.S。 UsersAdapter 是通常的 RecyclerView.Adapter.

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

fun loadUsers() {
    _users.value = demoData.toMutableList().apply { shuffle() }
}

toMutableList() 用数据创建一个新列表,查看源代码:

public fun <T> Collection<T>.toMutableList(): MutableList<T> {
    return ArrayList(this)
}

因此,与其获取初始值并且从不更新适配器,不如更新适配器中的列表并显示它。

viewModel.users.observe(this, Observer { users ->
    mAdapter.updateData(users)
})

如果你不是using ListAdapter,那么你可以这样定义这个方法:

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

    fun updateData(users: List<User>) {
        this.users = users
        notifyDataSetChanged()
    }
}

你也可以使用ListAdapter and submitList,你也会得到动画。