kotlin 不更新回收站中的项目的差异

jucafojl  于 2023-11-21  发布在  Kotlin
关注(0)|答案(1)|浏览(266)

我在将数据更新到回收器视图时遇到问题。

  1. class DiffUtilCallback : DiffUtil.ItemCallback<SectorItemDataUI>() {
  2. override fun areItemsTheSame(oldItem: SectorItemDataUI, newItem: SectorItemDataUI): Boolean {
  3. return oldItem.sectorId == newItem.sectorId
  4. }
  5. override fun areContentsTheSame(oldItem: SectorItemDataUI, newItem: SectorItemDataUI): Boolean {
  6. return oldItem == newItem
  7. }
  8. }

字符串
在viewModel中,我有liveData

  1. private val _sectorItem: MutableLiveData<MutableList<SectorItemDataUI>> =
  2. MutableLiveData()
  3. val sectorItem: LiveData<MutableList<SectorItemDataUI>> get() = _sectorItem


我有这个方法来更新值(只有温度或湿度)

  1. private fun updateMeasurementItem(measurement: List<NetworkMeasurement>) {
  2. val lastMeasurement = measurement.lastOrNull() ?: return
  3. _sectorItem.value = _sectorItem.value.also {
  4. it?.find { item -> item.temperatureSensorId == lastMeasurement.sensorId }?.temperature =
  5. lastMeasurement.value.toString()
  6. it?.find { item -> item.humiditySensorId == lastMeasurement.sensorId }?.humidity =
  7. lastMeasurement.value.toString()
  8. }
  9. }


在片段中,我将列表更新为TE recycler

  1. viewModel.sectorItem.observe(viewLifecycleOwner) {
  2. homeSectorAdapter.submitList(it)
  3. }


但是由于某些原因,回收器中的数据没有更新。
如果我创建了新的数据示例,回收器会正确地更新数据。但是我知道这不是一个好的做法,所以我不想这样做。

  1. private fun setupObservers() {
  2. viewModel.sectorItem.observe(viewLifecycleOwner) { data ->
  3. val aux: MutableList<SectorItemDataUI> = mutableListOf()
  4. data.forEach {
  5. aux.add(
  6. SectorItemDataUI(
  7. it.sectorId,
  8. it.sectorName,
  9. it.temperature,
  10. it.temperatureSensorId,
  11. it.humidity,
  12. it.humiditySensorId
  13. )
  14. )
  15. }
  16. homeSectorAdapter.submitList(aux)
  17. }
  18. }

q8l4jmvw

q8l4jmvw1#

你的代码看起来不错,可能是适配器中的update方法。
试试这样的东西:

  1. fun updateList(
  2. newItems: List<SectorItemDataUI>,
  3. readOnly: Boolean = false,
  4. ) {
  5. val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
  6. override fun getOldListSize(): Int {
  7. return [email protected]
  8. }
  9. override fun getNewListSize(): Int {
  10. return newItems.size
  11. }
  12. override fun areItemsTheSame(
  13. oldItemPosition: Int,
  14. newItemPosition: Int,
  15. ): Boolean {
  16. return ([email protected][oldItemPosition]::class == newItems[newItemPosition]::class) && ([email protected][oldItemPosition].sectorId == newItems[newItemPosition].sectorId)
  17. }
  18. override fun areContentsTheSame(
  19. oldItemPosition: Int,
  20. newItemPosition: Int,
  21. ): Boolean {
  22. return [email protected][oldItemPosition]::class == newItems[newItemPosition]::class && [email protected][oldItemPosition] == newItems[newItemPosition]
  23. }
  24. })
  25. with([email protected]) {
  26. clear()
  27. addAll(newItems)
  28. }
  29. diffResult.dispatchUpdatesTo(this)
  30. }

字符串
clear和addAll用于完全更新模型,diffUtil用于告诉适配器必须更新视图中的哪些项(仅完全更新内容或ViewHolder)。

展开查看全部

相关问题