ScalingLazyListState defaults to the center of the...
# compose-wear
y
ScalingLazyListState defaults to the center of the second item (index 1). You can tell it to instead start in the first item and even jn the ScalingLazyColumn parameters use the start of items.
i
i’ve tried to initialize the state with 0 like so:
Copy code
val scalingLazyState = remember { ScalingLazyListState(initialCenterItemIndex = 0) }
But it had no affect. Then i’ve tried in ScalingLazyColumn define
Copy code
autoCentering = AutoCenteringParams(itemIndex = 0)
Then i’ve also tried to add
Copy code
anchorType = ScalingLazyListAnchorType.ItemStart
Again, no effect.
found the solution, i just need to do this in ScalingLazyColum: autoCentering = null
s
I think setting initialCenterItemIndex on the state should work if you also set itemIndex in the AutoCenterParams. Have you tried calling rememberScalingLazyListState(initialCenterItemIndex = 0) to create the state?
i
@stevebower yep, tried that. Didn’t help
j
I have added an answer to stackoverflow - but posting here as well for good measure
Copy code
@Preview(device = Devices.WEAR_OS_SMALL_ROUND, showSystemUi = true)
@Composable
fun SingleItemSLCWithLongText(modifier: Modifier = Modifier) {
    val scalingLazyState = remember { ScalingLazyListState(initialCenterItemIndex = 0, initialCenterItemScrollOffset = 80) }
    val focusRequester = remember { FocusRequester() }

    Scaffold(
        modifier = modifier.background(Color.Black),
        positionIndicator = { PositionIndicator(scalingLazyListState = scalingLazyState) }
    ) {
        ScalingLazyColumn(
            modifier = Modifier.scrollableColumn(focusRequester, scalingLazyState),
            autoCentering = AutoCenteringParams(itemIndex = 0, itemOffset = 80),
            state = scalingLazyState,
            anchorType = ScalingLazyListAnchorType.ItemStart
        ) {
            item {
                Text(
                    longText,
                    Modifier
                        .padding(start = 16.dp, end = 16.dp, bottom = 48.dp),
                    textAlign = TextAlign.Center,
                )
            }
        }

    }
}
i
@John Nichol yeah, just tried your snippet. your solution seems to be the way to go. Thank you for sharing! @stevebower so basically initialCenterItemIndex works if you also set anchorType and also apply offset, this is what John did above (without the offset the scroll will jump to the top too much where you have the first half of your screen blank and the bottom half of the screen begins the first item)