How would I go about remembering my scroll positio...
# compose-wear
n
How would I go about remembering my scroll position so that I can navigating back to it?
LazyListState
has
firstVisibleItemIndex
, but it seems as if
ScalingLazyListState
doesn't.
y
rememberScalingLazyListState()
will do it. I think it's positioned from the center, but raise a bug if you find it's not restoring position.
n
Actually, I did not express myself properly. What I need is to get the scroll position of my
scalingLazyList
. I can't seem to do it without the
firstVisibleItemIndex
. Is there an example that I could use, or could you please point me in the right direction?
y
listState.centerItemIndex and listState.centerItemScrollOffset
This sample screen uses it, the sample is designed to explain the initial layout of SLC, which I frequently struggle with if using directly. https://github.com/google/horologist/blob/main/sample/src/main/java/com/google/android/horologist/scratch/ScratchActivity.kt
n
Fabulous, many thanks!
Ok, I understand why it's not working for me. It's because I am using the Horologist
ScalingLazyColumn
like this:
Copy code
columnState: ScalingLazyColumnState = ScalingLazyColumnDefaults.belowTimeText().create()
If I use the ordinary ScalingLazyColumn, it works out of the box. So I am stuck now.
y
I'll send you the code you need tomorrow. Horologist does this with navigation already. Just needs to be adapted for direct saveable use.
n
Ok, thank you, looking forward to it!
y
Try this
Copy code
@Composable
public fun rememberColumnState(factory: ScalingLazyColumnState.Factory = ScalingLazyColumnDefaults.belowTimeText()): ScalingLazyColumnState {
    val columnState = factory.create()

    columnState.state = rememberSaveable(saver = ScalingLazyListState.Saver) {
        columnState.state
    }

    return columnState
}
Usable like
Copy code
val columnState = rememberColumnState(
            ScalingLazyColumnDefaults.scalingLazyColumnDefaults(
                initialCenterIndex = initialOffset.index,
                initialCenterOffset = initialOffset.offset,
                autoCentering = autoCentering,
                anchorType = anchorType
            )
        )
n
Works perfectly! Many thanks. 👍