android-jetpack-composewear-osandroid-jetpackandroid-wear-2.0android-jetpack-compose-material3

Bug in default behavior of ScalingLazyColumn (Jetpack Compose Wear OS)


I'm using ScalingLazyColumn with a very long Text inside as follows:

@Preview(device = Devices.WEAR_OS_SMALL_ROUND, showSystemUi = true)
@Composable
fun Test(modifier: Modifier = Modifier) {
    val scalingLazyState = remember { ScalingLazyListState() }
    val focusRequester = remember { FocusRequester() }

    Scaffold(
        modifier = modifier,
        positionIndicator = { PositionIndicator(scalingLazyListState = scalingLazyState) }
    ) {
        ScalingLazyColumn(
            modifier = Modifier.scrollableColumn(focusRequester, scalingLazyState),
            state = scalingLazyState,
        ) {
            item {
                Text(
                    longText,
                    Modifier
                        .padding(top = 20.dp, start = 16.dp, end = 16.dp, bottom = 48.dp),
                    textAlign = TextAlign.Center,
                )
            }
        }

    }
}

val longText =
    "Take the plunge\n" +
            "\n" +
            "commit oneself to a course of action about which one is nervous.\n" +
            "\n" +
            "\"she wondered whether to enter for the race, but decided to take the plunge\"\n" +
            "\"They're finally taking the plunge and getting married.\"\n" +
            "\n" +
            "\n" +
            "plunge:\n" +
            "jump or dive quickly and energetically.\n" +
            "\"our daughters whooped as they plunged into the sea\"\n"

But for some reason when I launch the app the focus goes to the bottom of the text, instead of the beginning, which looks like a bug. I've tried playing with different parameters of ScalingLazyColumn (anchorType, autoCentering, scalingParams) to no avail.

demo

Any idea how to fix it and make the ScalingLazyColumn focus on the beginning of the first element when I launch the app?


Solution

  • Switching off autoCentering is an option, but I would try and avoid it in most cases as it will will make handling getting the padding right on different devices sizes more difficult and often results in being able to over scroll the list items either at the beginning or the end.

    I am not sure exactly what you want to achieve when you say that you want the focus to be on the start of the first item but the following should give you what you need.

    1. Set the state initial item to 0
    2. Set the anchor type to ScalingLazyListAnchorType.ItemStart
    3. Remove top padding from your item
    4. Apply an offset to the state initialItem initialCenterItemScrollOffset to shift the start of you item up a little.
    5. Optionally adjust the autoCentering to make sure that the limit of the scrolling matches the initial position selected in the state
    @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,
                    )
                }
            }
    
        }
    }
    

    Here is a screenshot of how the screen initially looks

    Initial screen