StaggeredGridLayout 更新数据时的奇怪行为

问题描述 投票:0回答:1

我正在更新从视图模型检索的适配器中的列表。我的适配器继承自ListAdapter,并且还实现了DiffUtil.ItemCallback

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val adapter = NotesAdapter { note ->
        navigateToDetail(note.id)
    }
    adapter.setHasStableIds(true)

    val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
    layoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS

    binding.recycler.adapter = adapter
    binding.recycler.layoutManager = layoutManager
    binding.recycler.addItemDecoration(SpacesItemDecoration(DEFAULT_NOTE_CARD_SPACING))
    
    viewLifecycleOwner.lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            viewModel.notesList.onEach { notes ->
                adapter.submitList(notes)
            }.launchIn(this)

            viewModel.foldersList.onEach { folders ->
                folders.forEach { folder ->
                    binding.chipGroup.addChip(folder.id.toInt(), folder.name)
                }
            }.launchIn(this)
        }
    }
}

为什么在 StaggeredGridLayout 中更新列表时会出现这个奇怪的错误?

https://imgur.com/a/uwljlrC

android android-recyclerview
1个回答
0
投票

问题在于使用

ListAdapter
DiffUtils
。但是,我不清楚 AsyncDiffer 在更新数据时表现如此奇怪的原因。

© www.soinside.com 2019 - 2024. All rights reserved.