【问题标题】:LiveData with a list带有列表的 LiveData
【发布时间】:2021-01-20 11:02:39
【问题描述】:

我想观察一个 MutableList,所以当从 MutableList 中添加或删除项目时,DiffUtil 将更新 RecycerView。我认为更新列表的最佳方法是使用 LiveData,但我无法在 MutableList 中添加或删除项目。

我一直在关注下面的这个代码实验室来尝试帮助我。

https://codelabs.developers.google.com/codelabs/kotlin-android-training-diffutil-databinding/#4

主要活动

class MainActivity : AppCompatActivity() {
    val list: LiveData<MutableList<User>>? = null
    var mAdapter = RVAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
        addUser()
        val rv = findViewById<RecyclerView>(R.id.recycler_view)
        rv.apply {
            LayoutManager = LinearLayoutManager(baseContext, LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
        }

        list?.observe(viewLifeCycleOwner, Observer {
            it?.let {
                mAdapter.submitList(it)
            }
        }
    }

    private fun addUser() {
        list.add(User("Shawn", 1)
        list.add(User("Shannon", 2)
        list.add(User("Steve", 3)
        list.add(User("Sara", 4)

    } 
}

用户数据类

data class User(val name: String, val accountNumber: Int) {
}

适配器

class RVAdapter : ListAdapter<User, RVAdapter.ViewHolder>(MyDiffCallback()) {

    class MyDiffCallback : DiffUtil.ItemCallback<User>() {
        override fun areItemsTheSame(oldItem: User, newItem: User): Boolean {
            return oldItem.name == newItem.name
        }

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

   ... 
}

这是我目前的代码,我无法从列表中添加或删除项目,并且 ViewLifecycleOwner 未定义。

【问题讨论】:

  • 在您发布的培训中,他们使用 ViewModel,您的样本中缺少它的任何特殊原因?像这样的 LiveData 在 ViewModel 中更合适,在 Activity 中它是没有意义的
  • 我一开始试图在没有 ViewModel 的情况下这样做,然后在以后添加它。

标签: android kotlin android-livedata android-diffutils mutablelist


【解决方案1】:

首先,您需要初始化您的实时数据并将您的列表发布到其中。您可以查看以下示例

class MainActivity : AppCompatActivity() {
    val list = MutableLiveData<List<User>?>()
    var mAdapter = RVAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
        addUser()
        val rv = findViewById<RecyclerView>(R.id.recycler_view)
        rv.apply {
            LayoutManager = LinearLayoutManager(baseContext, LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
        }

        list.observe(this, Observer {
            it?.let {
                mAdapter.submitList(it)
            }
        }
    }

    private fun addUser() {
        val newList = mutableListOf<User>()
        newList.add(User("Shawn", 1)
        newList.add(User("Shannon", 2)
        newList.add(User("Steve", 3)
        newList.add(User("Sara", 4)
        list.postValue(newList)
    } 
}

【讨论】:

  • 这是一个有趣的方法,在列表发布到 LiveData 后如何编辑(添加和删除)列表。
  • 我不知道你的用例是什么,但是 livedata 目前没用,你可以简单地使用 mutableList 并执行操作并将其提交给适配器。只需确保在将项目发送到适配器之前复制项目即可。 mAdapter.submitList(it.map{item -> item.copy()})
【解决方案2】:

也许这会有所帮助。你可以实现你想要的,但你总是必须将 LiveData 与 ViewModel 结合起来以获取数据并更新它。 Google 建议这样做。

class MainActivity : AppCompatActivity() {
    val list: LiveData<MutableList<User>> = MutableLiveData<List<User>>().apply{
       postValue(mutableListOf<User>())
    }
    var mAdapter = RVAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
        addUser()
        val rv = findViewById<RecyclerView>(R.id.recycler_view)
        rv.apply {
            LayoutManager = LinearLayoutManager(baseContext, LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
        }

        list?.observe(viewLifeCycleOwner, Observer {
            it?.let {
                mAdapter.submitList(it)
            }
        }
    }

    private fun addUser() {
        
        val userList = list.getValue()
        userList .add(User("Shawn", 1)
        userList .add(User("Shannon", 2)
        userList .add(User("Steve", 3)
        userList .add(User("Sara", 4)
        list.postValue(userList)

    } 

    private fun editUser() {
       val userList = list.getValue()
        userList.add(User("Shawn", 21)
        
        list.postValue(userList)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2019-02-04
    相关资源
    最近更新 更多