kotlin LazyColumn项目在删除某些项目后仍保留在相同位置

tv6aics1  于 2022-12-19  发布在  Kotlin
关注(0)|答案(2)|浏览(215)

我有一个带有滑动删除功能的LazyColumn,当我滑动一个项目时,它会被viewModel删除,问题是如果我把项目滑动走,LazyColumn不会更新其他项目的位置(如GIF所示)。

下面是我的代码实现:

@ExperimentalMaterialApi
@Composable
fun Screen() {
    val livedata = viewModel.itemsLiveData.observeAsState()
    val stateList = remember { mutableStateListOf<Data>() }

    stateList.addAll(livedata.value!!)
    SwipableLazyColumn(stateList)
}

@ExperimentalMaterialApi
@Composable
fun SwipableLazyColumn(
    stateList: SnapshotStateList<Data>
) {
    LazyColumn {
        items(items = stateList) { item ->
            val dismissState = rememberDismissState()
            if (dismissState.isDismissed(EndToStart) || dismissState.isDismissed(StartToEnd)) {
                viewModel.swipeToDelete(item)
            }
            SwipeToDismiss(
                state = dismissState,
                directions = setOf(StartToEnd, EndToStart),
                dismissThresholds = {
                    FractionalThreshold(0.25f)
                },
                background = {},
                dismissContent = {
                    MyData(item)
                }
            )
        }
    }
}

我按照here的建议使用SnapshotStateList,但我没有使用swapList,因为它会清除所有项

视图模型

class MyViewModel @Inject internal constructor(
    private val itemRepository: ItemRepository
) : BaseViewModel(), LifecycleObserver {

    private val itemsList = mutableListOf<MyData>()

    private val _itemsLiveData = MutableLiveData<List<MyData>>()
    val itemsLiveData: LiveData<List<MyData>> = _itemsLiveData

    init {
        loadItems()
    }

    private fun loadItems() {
        viewModelScope.launch {
            itemRepository.getItems().collect {
                when (it) {
                    is Result.Success -> onItemsLoaded(it.data)
                    is Result.Error -> {
                        onItemsLoaded(emptyList())
                    }
                }
            }
        }
    }

    private fun onItemsLoaded(itemsList: List<MyData>) {
        itemsList.clear()
        itemsList.addAll(notifications)

        _itemsLiveData.value = if (itemsList.isNotEmpty()) {
            itemsList
        } else {
            null
        }
    }

    fun swipeToDelete(item: MyData) {
        if (itemsList.size == 0) return
        viewModelScope.launch {
            when (
                val result =
                    itemRepository.deletelItem(item)
            ) {
                is Result.Success -> {
                    onItemDeleted(item)
                }
                is Result.Error -> {
                    showSnackBar(
                        "error"
                    )
                }
            }
        }
    }

    private fun onItemDeleted(item: MyData) {
        itemsList.remove(item)
        _itemsLiveData.value = itemsList
    }
}
rqenqsqc

rqenqsqc1#

您应该刷新viemodel内的列表(删除项目)并在此处返回修改后的列表

var tempList = itemList
ItemList.clear() 
ItemList.addAll(tempList)
gopyfrb3

gopyfrb32#

您需要为LazyColumnitems提供key
默认情况下,每个项的状态都是根据项在列表中的位置设置的。但是,如果数据集发生更改,这可能会导致问题,因为更改位置的项实际上会丢失任何记忆的状态。

示例

LazyColumn {
    items(
        items = stateList,
        key = { _, listItem ->
            listItem.hashCode()
        },
    ) { item ->
        // As it is ...
    }
}

参考文献

相关问题