Is there a generic way to know when the scroll sta...
# compose
j
Is there a generic way to know when the scroll state of a scrollable composable is at its topmost position? Thing is Lazy lists hold scroll state in the 
LazyListState
 type whilst composables that scroll using modifiers hold scroll state in the 
ScrollState
  type. Even though both types implement  
ScrollableState
  the way they expose the current scroll position differs:
LazyListState
 exposes 
firstVisibleItemIndex
 and 
firstVisibleItemScrollOffset
 for this purpose, while 
ScrollState
 exposes 
value
 instead. This is making it more difficult to implement a generic solution that works with both e.g. a 
LazyColumn
 and a 
Column
 which uses the 
verticalScroll
 modifier. In case you’re wondering what I’d like to accomplish you’ll find more info in the thread.
1
I'd like to change the `topBar`'s elevation inside a 
Scaffold
 depending on whether its main 
content
 has been scrolled all the way to the top or not. I managed to do that by hoisting the `LazyColumn`'s 
LazyListState
 up into the 
Scaffold
 but I'd like a generic solution that works for any scrollable composable that could be hosted in the Scaffold's content. Do you think it's possible?
I’ve also tried with the nested scroll subsystem but it seems it only informs about scroll events, and not scroll position. So it’s not possible to extract a scroll position from it.
a
You can increase the elevation in
onPostScroll
when there's offset available in a downwards scroll, and decrease it in
onPreScroll
when it's a upwards scroll.
🙏 1
j
This actually works! But I still don’t understand why 🙂. I think I’m missing something about the nested scrolling subsystem.
t
I think there is this nestedScroll modifier for such use cases. But i think it is not working reliable for now to identify the exact scrolling position: https://developer.android.com/reference/kotlin/androidx/compose/ui/input/nestedscroll/package-summary#(androidx.compose.ui.[…]estedScrollDispatcher)
j
Yes it works @Timo Drick, that’s what @Albert Chang suggested and that’s what I’ve done in the end. I still don’t fully understand how the offsets in those callbacks work, but I was able to accomplish what I originally asked for.
👍 1