TransformingLazyColumnFirstLayoutItemProvider

fun interface TransformingLazyColumnFirstLayoutItemProvider


Provides the first item to layout for TransformingLazyColumn.

During a measurement pass, TransformingLazyColumn uses the item returned by this provider as the initial placement reference. TransformingLazyColumn measures this item and places its requested ItemInfo.itemEdge visual edge at the exact screen coordinate defined by ItemInfo.offset, prior to accounting for active scroll deltas. Once this initial item is positioned, all other visible items are sequentially composed and placed above and below it.

Providing this interface allows controlling how the list places its children during content updates like additions, removals, or item size changes.

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.PaddingValues
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.compose.ui.util.fastFirstOrNull
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
import androidx.wear.compose.foundation.lazy.TransformingLazyColumnFirstLayoutItemProvider
import androidx.wear.compose.foundation.lazy.TransformingLazyColumnFirstLayoutItemProvider.ItemEdge
import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
import androidx.wear.compose.material.Text
import androidx.wear.compose.material3.CardDefaults
import androidx.wear.compose.material3.SurfaceTransformation
import androidx.wear.compose.material3.TitleCard
import androidx.wear.compose.material3.lazy.rememberTransformationSpec
import androidx.wear.compose.material3.lazy.transformedHeight

val state = rememberTransformingLazyColumnState()
val transformationSpec = rememberTransformationSpec()
var expandedItemIndex by remember { mutableIntStateOf(-1) }

// This sample demonstrates how to use the provider API to control the direction of content
// shifting. By default, TransformingLazyColumn uses the center item as the layout reference.
// This means that if an item above the center expands, it pushes content upwards;
// if below, it pushes downwards.
//
// Here, we fix the Bottom/End edge of the clicked item regardless of its position on screen,
// so that when its animated content appears, the card predictably expands *upwards* every time.
val upwardExpandingItemProvider =
    remember(state) {
        TransformingLazyColumnFirstLayoutItemProvider { centerItem ->
            val item = expandedItemIndex

            // Yield to the standard layout behavior during active scrolls.
            // This avoids custom layout overhead and ensures the [TransformingLazyColumn]
            // tracks the user's scroll gesture using its default center layout reference.
            if (item == -1 || state.isScrollInProgress) {
                return@TransformingLazyColumnFirstLayoutItemProvider centerItem
            }

            // Look up the item's offset from state.layoutInfo (which holds the details
            // from the previous measure pass) to maintain its visual position in the current
            // pass.
            state.layoutInfo.visibleItems
                .fastFirstOrNull { visibleItem -> visibleItem.index == item }
                ?.let { visibleItem ->
                    TransformingLazyColumnFirstLayoutItemProvider.ItemInfo(
                        key = visibleItem.key,
                        index = visibleItem.index,
                        // Pin the bottom edge of the item
                        itemEdge = ItemEdge.End,
                        // Calculate the exact bottom offset from the previous pass
                        offset = visibleItem.offset + visibleItem.transformedHeight,
                    )
                } ?: centerItem
        }
    }

TransformingLazyColumn(
    state = state,
    contentPadding = PaddingValues(horizontal = 20.dp),
    firstLayoutItemProvider = upwardExpandingItemProvider,
) {
    items(count = 10, key = { it }) { cardIndex ->
        val isExpanded = expandedItemIndex == cardIndex
        TitleCard(
            onClick = { expandedItemIndex = cardIndex },
            modifier =
                Modifier.minimumVerticalContentPadding(
                        CardDefaults.minimumVerticalListContentPadding
                    )
                    .fillMaxWidth()
                    .transformedHeight(this, transformationSpec),
            transformation = SurfaceTransformation(transformationSpec),
            title = { Text("Card $cardIndex") },
            subtitle = {
                AnimatedVisibility(isExpanded) { Text("Expanded content is available here") }
            },
            content = { Text("Tap to expand") },
        )
    }
}

Summary

Nested types

Represents the visual edge of ItemInfo (Start or End) to which the offset refers.

Holds information about the first item to layout in TransformingLazyColumn.

Public functions

getFirstLayoutItem

Added in 1.7.0-alpha05
fun getFirstLayoutItem(
    centerItem: TransformingLazyColumnFirstLayoutItemProvider.ItemInfo
): TransformingLazyColumnFirstLayoutItemProvider.ItemInfo

Returns the ItemInfo for the first item to layout in TransformingLazyColumn.

Note: This method is executed internally inside a Snapshot.withoutReadObservation block. Any Compose state reads performed inside this callback will not trigger layout observation.

If the returned ItemInfo cannot be fully resolved (e.g., the key is not found or the index is out of bounds), TransformingLazyColumn falls back:

  • If the ItemInfo.key is not found, it falls back to the ItemInfo.index.

  • The final resolved index is coerced to stay within the valid list bounds.

Parameters
centerItem: TransformingLazyColumnFirstLayoutItemProvider.ItemInfo

The ItemInfo that TransformingLazyColumn would currently use for the first layout item if no provider is supplied. Returning this item preserves the default layout behavior. In most cases, this is the item closest to the center of the viewport from TransformingLazyColumnLayoutInfo.visibleItems.

Returns
TransformingLazyColumnFirstLayoutItemProvider.ItemInfo

The ItemInfo of the item to use as the first item to layout.