Tiago Nunes

    Tiago Nunes

    1 year ago
    Hey everyone, is there no way to get the scroll offset from the start of a LazyList?
    val lazyListState = rememberLazyListState()
    Here
    lazyListState
    only has the properties
    firstVisibleItemScrollOffset
    and
    firstVisibleItemIndex
    . The
    firstVisibleItemScrollOffset
    resets to 0 after each item scroll. I know I can do
    firstVisibleItemScrollOffset + firstVisibleItemIndex ª itemHeight
    , but sometimes getting the
    itemHeight
    is tricky or impossible
    d

    dimsuz

    1 year ago
    Not sure if that would be helpful for you case, but you can use
    .nestedScroll(connection)
    modifier on lazy list parent and provide it with connection which will be able to "watch" the list scroll deltas.
    Tiago Nunes

    Tiago Nunes

    1 year ago
    @dimsuz Thanks! That’s exactly what I wanted! And the docs are awesome https://developer.android.com/reference/kotlin/androidx/compose/ui/input/nestedscroll/package-summary
    Nevermind, I’m having trouble getting the total scroll offset value based on the scroll deltas 😕
    d

    dimsuz

    1 year ago
    You could sum them up perhaps? But yeah, this was discussed before in this channel (used to find it by searching) and it was said that due to the different height of items and the lazy building nature it isn't quite possible to correctly know the offset. If I remember correctly. Try searching this channel for
    firstVisibleItemScrollOffset
    , you should be able to find those discussions.
    Tiago Nunes

    Tiago Nunes

    1 year ago
    I’ve figured out a way:
    object : NestedScrollConnection {
    
        override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
            val delta = available.y
            scroll.value -= delta
            return Offset.Zero
        }
    
        override fun onPostScroll(
            consumed: Offset,
            available: Offset,
            source: NestedScrollSource
        ): Offset {
            val delta = available.y
            scroll.value += delta
            return Offset.Zero
        }
    It works, but it’s probably not very efficient, since it’s changing the scroll value a lot