https://kotlinlang.org logo
#compose
Title
# compose
s

sashjakk

11/24/2020, 10:08 AM
Hello, I’ve been playing around with lazy column and scroll positions. I am able to get first visible item & it’s offset … but nothing about last visible item 🤔 adding scrollable modifier throws an exception with nested scrolling not allowed 😅 any ideas how to get last visible item & offset ? Or maybe its possible to calculate by hand at least? I need to draw some shadows for toolbar and bottom bar based on scrolling
a

André Kindwall

11/24/2020, 11:22 AM
This solution updates the state of
lastVisibleItemIndex
when each list item is composed. I dont know, but I guess this solution is bad practice
Copy code
val state = rememberLazyListState()
val (lastVisibleItemIndex, setLastVisibleItemIndex) =
    remember(state.firstVisibleItemIndex, items) { mutableStateOf(0) }
LazyColumnForIndexed(items = items, state = state) { index, item ->
    Text("item $item")
    if (index > lastVisibleItemIndex) {
        setLastVisibleItemIndex(index)
    }
}
Text("First visible ${state.firstVisibleItemIndex}")
Text("Last visible $lastVisibleItemIndex")
s

sashjakk

11/24/2020, 11:28 AM
I see… Wouldn’t it cause troubles, cause drawing each item in list updates state… which will trigger recomposition again?
a

André Kindwall

11/24/2020, 11:34 AM
Yeah I think so as well, but I'm still a jetpack compose noob so I haven't learned yet how recomposing really works
2 Views