TransformingLazyColumnAnchorType

value class TransformingLazyColumnAnchorType


Represents the part of the item that should remain fixed when it is requested as a layout anchor viaTransformingLazyColumnState.requestAnchorItem.

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
import androidx.wear.compose.foundation.lazy.TransformingLazyColumnAnchorType
import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
import androidx.wear.compose.material.Text
import androidx.wear.compose.material3.Button
import androidx.wear.compose.material3.ButtonDefaults
import androidx.wear.compose.material3.SurfaceTransformation
import androidx.wear.compose.material3.lazy.rememberTransformationSpec
import androidx.wear.compose.material3.lazy.transformedHeight

// This sample demonstrates how to use requestAnchorItem to control the expansion direction
// of an item. When the "input_box" Button is clicked, its text content grows, causing its
// intrinsic height to increase. By requesting ItemBottom as the layout anchor immediately
// before the state change, the visual bottom edge of the button remains pinned in place on
// the screen, and the new height expands upwards.
val state = rememberTransformingLazyColumnState()
val transformationSpec = rememberTransformationSpec()

var textLines by remember { mutableIntStateOf(1) }

TransformingLazyColumn(
    state = state,
    contentPadding = PaddingValues(20.dp),
    modifier = Modifier.fillMaxSize(),
) {
    items(3, key = { "item_$it" }) { index ->
        Button(
            onClick = {},
            modifier =
                Modifier.fillMaxWidth()
                    .transformedHeight(this, transformationSpec)
                    .minimumVerticalContentPadding(
                        ButtonDefaults.minimumVerticalListContentPadding
                    ),
            transformation = SurfaceTransformation(transformationSpec),
        ) {
            Text(text = "Item $index")
        }
    }

    // The expanding item
    item(key = "input_box") {
        Button(
            onClick = {
                state.requestAnchorItem(
                    key = "input_box",
                    anchorType = TransformingLazyColumnAnchorType.ItemBottom,
                )
                textLines = if (textLines < 4) textLines + 1 else 1
            },
            modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec),
            transformation = SurfaceTransformation(transformationSpec),
        ) {
            val text = List(textLines) { "Input line ${it + 1}" }.joinToString("\n")
            Text(text = text)
        }
    }

    items(3, key = { "item_${it + 4}" }) { index ->
        Button(
            onClick = {},
            modifier =
                Modifier.fillMaxWidth()
                    .transformedHeight(this, transformationSpec)
                    .minimumVerticalContentPadding(
                        ButtonDefaults.minimumVerticalListContentPadding
                    ),
            transformation = SurfaceTransformation(transformationSpec),
        ) {
            Text(text = "Item ${index + 4}")
        }
    }
}

Summary

Public companion properties

TransformingLazyColumnAnchorType

Anchors the visual bottom edge of the item to its current position on the screen.

TransformingLazyColumnAnchorType

Anchors the visual top edge of the item to its current position on the screen.

Public functions

open String

Public companion properties

ItemBottom

val ItemBottomTransformingLazyColumnAnchorType

Anchors the visual bottom edge of the item to its current position on the screen. If the item's height grows, it will expand upwards. This behavior is absolute and remains the same regardless of the list's reverseLayout direction.

ItemTop

val ItemTopTransformingLazyColumnAnchorType

Anchors the visual top edge of the item to its current position on the screen. If the item's height grows, it will expand downwards. This behavior is absolute and remains the same regardless of the list's reverseLayout direction.

Public functions

toString

open fun toString(): String