2

I have a LazyColumn that takes a list from my Room database.

I am creating a button that can re arrange the list from newest first, or oldest first. The problem I'm having is that when I rearrange the list, the LazyColumns view drops to the bottom of the LazyColumn. I do NOT want the list view to change during the list change. I am using a key for the list which is where I suspect my issue is coming from. When I disable the key, this is not an issue however, that comes with its own issues so I cannot disable it permanently. Does anyone know and easy fix to this?

my composable ->

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MainScreen(navController: NavController, notesViewModel: NotesViewModel) {

    val myUiState by notesViewModel.uiState.collectAsState()
    val multiDelete = remember { mutableStateListOf<Note>() }
    val scope = rememberCoroutineScope()
    val state = rememberLazyListState()

    Surface {
        Column {
            Row {
                FloatingActionButton(onClick = { notesViewModel.updateStates(true) }) {}

                FloatingActionButton(onClick = { notesViewModel.updateStates(false) }) {}

                NewNote(navController)
                if(multiDelete.isNotEmpty()){
                    FloatingActionButton(
                        onClick = {
                            scope.launch {
                                notesViewModel.deleteSelected(multiDelete)
                                delay(50)
                                multiDelete.clear()
                            }
                        }
                    ) { Image(imageVector = Icons.Filled.Delete, contentDescription = "this") }
                }
            }

            LazyColumn(
                state = state,
                horizontalAlignment = Alignment.CenterHorizontally,
                contentPadding = PaddingValues(vertical = 10.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp),
                modifier = Modifier
                    .background(color = Color.Gray)
                    .fillMaxSize()
                    .focusRequester(FocusRequester()),

            ) {
                items(
                    if(myUiState.toggle) myUiState.allNotes else myUiState.allNotes.reversed(),
                    key = {notes -> notes.uid!!}
                ) {
                    notes ->
                    Column(
                        modifier = Modifier.animateItemPlacement()
                    ) {
                        ConsoleCards(
                            note = notes,
                            onDeleteClick = {
                                notesViewModel.delete(notes)
                            },
                            onLongPress = {
                                if(multiDelete.contains(notes)) multiDelete.remove(notes) else multiDelete.add(notes)
                            },
                            onEditClick = {
                                notesViewModel.uid(notes.uid!!)
                                notesViewModel.header(notes.header!!)
                                notesViewModel.note(notes.note!!)
                                navController.navigate(route = PageNav.AddNote.name)
                            }
                        )
                    }
                }
            }
        }
    }
}
JayDee
  • 83
  • 4

1 Answers1

1

This may not be the best solution. Theres also a similar issue like this and this

itemsIndexed(
    items = checkItems.sortedBy { it.checked.value },
    key = { index, item -> if (index == 0) index else item.id }
) { index, entry ->
    ...
}
z.g.y
  • 5,512
  • 4
  • 10
  • 36
  • 1
    Thank you, this only really kind of sorted the issue but I guess it will do for now as I don't think there is an actual resolution to it. the issue with this is that depending on the size of the content and the position of the scroll view, it can still snap to weird positions, in addition to that the fact that the animation for spot one doesn't work is kinda weird. – JayDee Dec 07 '22 at 11:15
  • Yep its just a patch, not an actual solution, in one of those S.O links, there was a comment pointing to a google issue tracker.. you might as well +1 the issue tracker and lets just see what will be their resolution. Thank you for upvoting the answer – z.g.y Dec 07 '22 at 11:17