mutableStateOf mutablelist 在拖动 lazycolumn 中的项时不更新 ui

mutableStateOf mutablelist not updating ui when items in lazycolumn was dragged

提问人:ltvie 提问时间:11/11/2023 更新时间:11/11/2023 访问量:22

问:

我有代码可以在 LazyColumn 中实现拖放。到目前为止,MutableList 中的数据会根据拖动的项目而变化,但不会触发数据顺序的更改,因此在 UI 中没有更新

var songList = remember { mutableListOf<SongPlayer>() }

val stateDrag = rememberReorderableLazyListState(
    onMove = { from, to ->
        Log.v("candrage move", from.index.toString() +" to => "+to.index)
        songList = songList.toMutableList().apply {
            add(songList.indexOfFirst {
                it.mediaId == (to.key as SongPlayer).mediaId }, removeAt(songList.indexOfFirst { it.mediaId == (from.key as SongPlayer).mediaId }))
        }
        Log.v("songlist", songList) // <--------- this data updated base on user dragged
}, canDragOver = { draggedOver, _ ->
    true
})

LazyColumn(
    state = stateDrag.listState,
    modifier = Modifier.then(Modifier.reorderable(stateDrag))
) {
    Log.v("moveNewSonglistItems", songList.map { it.songName }) // <------- this is NOT TRIGGERED
    items(songList, { it }) { item ->
        ReorderableItem(stateDrag, item) { isDragging ->
            val elevation = animateDpAsState(if (isDragging) 2.dp else 0.dp)
            Box(
                modifier = Modifier
                    .shadow(elevation.value)
                    .padding(vertical = 1.dp)
                    .detectReorder(stateDrag)
            ) {
                SongListItem(
                    songPlayer = item,
                    showCover = true,
                    showDrag = true,
                    onSongClick = {
                    },
                    onOptionClick = {
                    }
                )
            }
        }
    }
}

如何解决这个问题?

科特林 android-jetpack-compose 安卓喷气背包

评论

0赞 Edric 11/11/2023
您应该考虑改用mutableStateListOf
0赞 ltvie 11/11/2023
啊......我的错误,我不够小心。现在工作正常..

答: 暂无答案