Enable Auto-scrolling LazyColumn to the bottom when reverseLayout = false

I have a LazyColumn holding some Items. I listen a Flow from Room and everything works great when reverseLayout = true. When new Item get inserted into the database, the LazyColumn scrolls to the bottom and shows last Item just fine. But reverseLayout = true inverts the headers too. So I disabled it with reverseLayout = false. This does not work in that when Item get inserted, it is shown on the list but the list does not get scrolled to show the bottom item. I have to manually scroll.

How can I scroll to the bottom Item when the LazyColumn get recomposed on items change?

I don't think I need to put the code since the problem happens with any use of LazyColumn but if that is important let me know and I can redact the code and post them!

1 answer

  • answered 2022-05-04 10:28 Thracian

    In a messaging app i scroll to bottom using size of items and LazyListState

    as

    coroutineScope.launch {
        scrollState.animateScrollToItem(messages.size - 1)
    }
    

    and whenever user inputs a message it gets added to

     val messages = remember { mutableStateListOf<ChatMessage>() }
    

    you can call scroll in a LaunchedEffect(messages.size){...} for it to be invoked only when recomposition happens and item count has changed

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum