androidx.wear.compose.foundation.lazy


Interfaces

ScalingLazyListItemInfo

Contains useful information about an individual item in a ScalingLazyColumn.

ScalingLazyListItemScope

Receiver scope being used by the item content parameter of ScalingLazyColumn.

ScalingLazyListLayoutInfo

Contains useful information about the currently displayed layout state of ScalingLazyColumn.

ScalingLazyListScope

Receiver scope which is used by ScalingLazyColumn.

ScalingParams

Parameters to control the scaling of the contents of a ScalingLazyColumn.

Classes

AutoCenteringParams

Parameters to determine which list item and offset to calculate auto-centering spacing for.

ScalingLazyListAnchorType
ScalingLazyListState

A state object that can be hoisted to control and observe scrolling.

Objects

ScalingLazyColumnDefaults

Contains the default values used by ScalingLazyColumn

Annotations

ScalingLazyScopeMarker

DSL marker used to distinguish between lazy layout scope and the item scope.

Top-level functions summary

Unit
@Composable
ScalingLazyColumn(
    modifier: Modifier,
    state: ScalingLazyListState,
    contentPadding: PaddingValues,
    reverseLayout: Boolean,
    verticalArrangement: Arrangement.Vertical,
    horizontalAlignment: Alignment.Horizontal,
    flingBehavior: FlingBehavior,
    userScrollEnabled: Boolean,
    scalingParams: ScalingParams,
    anchorType: ScalingLazyListAnchorType,
    autoCentering: AutoCenteringParams?,
    rotaryBehavior: RotaryBehavior?,
    content: ScalingLazyListScope.() -> Unit
)

A scrolling scaling/fisheye list component that forms a key part of the Wear Material Design language.

ScalingLazyListState
@Composable
rememberScalingLazyListState(
    initialCenterItemIndex: Int,
    initialCenterItemScrollOffset: Int
)

Creates a ScalingLazyListState that is remembered across compositions.

Extension functions summary

inline Unit
<T : Any?> ScalingLazyListScope.items(
    items: Array<T>,
    noinline key: ((item) -> Any)?,
    crossinline itemContent: @Composable ScalingLazyListItemScope.(item) -> Unit
)

Adds an array of items.

inline Unit
<T : Any?> ScalingLazyListScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)?,
    crossinline itemContent: @Composable ScalingLazyListItemScope.(item) -> Unit
)

Adds a list of items.

inline Unit
<T : Any?> ScalingLazyListScope.itemsIndexed(
    items: Array<T>,
    noinline key: ((index: Int, item) -> Any)?,
    crossinline itemContent: @Composable ScalingLazyListItemScope.(index: Int, item) -> Unit
)

Adds an array of items where the content of an item is aware of its index.

inline Unit
<T : Any?> ScalingLazyListScope.itemsIndexed(
    items: List<T>,
    noinline key: ((index: Int, item) -> Any)?,
    crossinline itemContent: @Composable ScalingLazyListItemScope.(index: Int, item) -> Unit
)

Adds a list of items where the content of an item is aware of its index.

Top-level functions

@Composable
fun ScalingLazyColumn(
    modifier: Modifier = Modifier,
    state: ScalingLazyListState = rememberScalingLazyListState(),
    contentPadding: PaddingValues = PaddingValues(horizontal = 10.dp),
    reverseLayout: Boolean = false,
    verticalArrangement: Arrangement.Vertical = Arrangement.spacedBy( space = 4.dp, alignment = if (!reverseLayout) Alignment.Top else Alignment.Bottom ),
    horizontalAlignment: Alignment.Horizontal = Alignment.CenterHorizontally,
    flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
    userScrollEnabled: Boolean = true,
    scalingParams: ScalingParams = ScalingLazyColumnDefaults.scalingParams(),
    anchorType: ScalingLazyListAnchorType = ScalingLazyListAnchorType.ItemCenter,
    autoCentering: AutoCenteringParams? = AutoCenteringParams(),
    rotaryBehavior: RotaryBehavior? = RotaryDefaults.scrollBehavior(scrollableState = state),
    content: ScalingLazyListScope.() -> Unit
): Unit

A scrolling scaling/fisheye list component that forms a key part of the Wear Material Design language. Provides scaling and transparency effects to the content items.

ScalingLazyColumn is designed to be able to handle potentially large numbers of content items. Content items are only materialized and composed when needed.

If scaling/fisheye functionality is not required then a LazyColumn should be considered instead to avoid any overhead of measuring and calculating scaling and transparency effects for the content items.

This overload supports rotary input. Rotary input allows users to scroll the content of the ScalingLazyColumn - by using a crown or a rotating bezel on their Wear OS device. It can be modified with rotaryBehavior param. If scroll with fling is required use RotaryDefaults.scrollBehavior. If snapping is required use RotaryDefaults.snapBehavior. Note that rotary scroll and touch scroll should be aligned. If rotaryBehavior is set for snap (using RotaryDefaults.snapBehavior), flingBehavior should be set for snap as well (using ScalingLazyColumnDefaults.snapFlingBehavior). This composable uses rememberActiveFocusRequester as FocusRequester for rotary support. It requires that this ScalingLazyColumn should be wrapped by HierarchicalFocusCoordinator. By default HierarchicalFocusCoordinator is already implemented in BasicSwipeToDismissBox, which is a part of material Scaffold - meaning that rotary will be able to request a focus without any additional changes. Another FocusRequester can be added through Modifier chain by adding .focusRequester(focusRequester). Do not call focusable() after it as this will reset the focusRequester chain and rotary support will not be available.

Example of a ScalingLazyColumn with default parameters:

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.ui.Modifier
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.material.Chip
import androidx.wear.compose.material.ChipDefaults
import androidx.wear.compose.material.ListHeader
import androidx.wear.compose.material.Text

ScalingLazyColumn(
    modifier = Modifier.fillMaxWidth()
) {
    item {
        ListHeader {
            Text(text = "List Header")
        }
    }
    items(20) {
        Chip(
            onClick = { },
            label = { Text("List item $it") },
            colors = ChipDefaults.secondaryChipColors()
        )
    }
}

Example of a ScalingLazyColumn using ScalingLazyListAnchorType.ItemStart anchoring, in this configuration the edge of list items is aligned to the center of the screen. Also this example shows scrolling to a clicked list item with ScalingLazyListState.animateScrollToItem:

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.wear.compose.foundation.lazy.AutoCenteringParams
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.ScalingLazyListAnchorType
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
import androidx.wear.compose.material.Chip
import androidx.wear.compose.material.ChipDefaults
import androidx.wear.compose.material.ListHeader
import androidx.wear.compose.material.Text

val coroutineScope = rememberCoroutineScope()
val itemSpacing = 6.dp
// Line up the gap between the items on the center-line
val scrollOffset = with(LocalDensity.current) {
    -(itemSpacing / 2).roundToPx()
}
val state = rememberScalingLazyListState(
    initialCenterItemIndex = 1,
    initialCenterItemScrollOffset = scrollOffset
)

ScalingLazyColumn(
    modifier = Modifier.fillMaxWidth(),
    anchorType = ScalingLazyListAnchorType.ItemStart,
    verticalArrangement = Arrangement.spacedBy(itemSpacing),
    state = state,
    autoCentering = AutoCenteringParams(itemOffset = scrollOffset)
) {
    item {
        ListHeader {
            Text(text = "List Header")
        }
    }
    items(20) {
        Chip(
            onClick = {
                coroutineScope.launch {
                    // Add +1 to allow for the ListHeader
                    state.animateScrollToItem(it + 1, scrollOffset)
                }
            },
            label = { Text("List item $it") },
            colors = ChipDefaults.secondaryChipColors()
        )
    }
}

Example of a ScalingLazyColumn with snap of items to the viewport center:

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.ui.Modifier
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.ScalingLazyColumnDefaults
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
import androidx.wear.compose.foundation.rotary.RotaryDefaults
import androidx.wear.compose.material.Chip
import androidx.wear.compose.material.ChipDefaults
import androidx.wear.compose.material.ListHeader
import androidx.wear.compose.material.Text

val state = rememberScalingLazyListState()
ScalingLazyColumn(
    rotaryBehavior = RotaryDefaults.snapBehavior(state = state),
    flingBehavior = ScalingLazyColumnDefaults.snapFlingBehavior(state = state),
    modifier = Modifier.fillMaxWidth(),
    state = state,
) {
    item {
        ListHeader {
            Text(text = "List Header")
        }
    }
    items(20) {
        Chip(
            onClick = { },
            label = { Text("List item $it") },
            colors = ChipDefaults.secondaryChipColors()
        )
    }
}

Example of a ScalingLazyColumn where autoCentering has been disabled and explicit contentPadding provided to ensure there is space above the first and below the last list item to allow them to be scrolled into view on circular screens:

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.material.Chip
import androidx.wear.compose.material.ChipDefaults
import androidx.wear.compose.material.ListHeader
import androidx.wear.compose.material.Text

ScalingLazyColumn(
    modifier = Modifier.fillMaxWidth(),
    contentPadding = PaddingValues(top = 20.dp, bottom = 20.dp),
    autoCentering = null
) {
    item {
        ListHeader {
            Text(text = "List Header")
        }
    }
    items(20) {
        Chip(
            onClick = { },
            label = { Text("List item $it") },
            colors = ChipDefaults.secondaryChipColors()
        )
    }
}

For more information, see the Lists guide.

Parameters
modifier: Modifier = Modifier

The modifier to be applied to the component

state: ScalingLazyListState = rememberScalingLazyListState()

The state of the component

contentPadding: PaddingValues = PaddingValues(horizontal = 10.dp)

The padding to apply around the contents

reverseLayout: Boolean = false

reverse the direction of scrolling and layout, when true items will be composed from the bottom to the top

verticalArrangement: Arrangement.Vertical = Arrangement.spacedBy( space = 4.dp, alignment = if (!reverseLayout) Alignment.Top else Alignment.Bottom )

The vertical arrangement of the layout's children. This allows us to add spacing between items and specify the arrangement of the items when we have not enough of them to fill the whole minimum size

horizontalAlignment: Alignment.Horizontal = Alignment.CenterHorizontally

the horizontal alignment applied to the items

flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior()

Logic describing fling behavior for touch scroll. If snapping is required use ScalingLazyColumnDefaults.snapFlingBehavior. Note that when configuring fling or snap behavior, this flingBehavior parameter and the rotaryBehavior parameter that controls rotary scroll are expected to produce similar list scrolling. For example, if rotaryBehavior is set for snap (using RotaryDefaults.snapBehavior), flingBehavior should be set for snap as well (using ScalingLazyColumnDefaults.snapFlingBehavior)

userScrollEnabled: Boolean = true

whether the scrolling via the user gestures or accessibility actions is allowed. You can still scroll programmatically using the state even when it is disabled.

scalingParams: ScalingParams = ScalingLazyColumnDefaults.scalingParams()

The parameters to configure the scaling and transparency effects for the component

anchorType: ScalingLazyListAnchorType = ScalingLazyListAnchorType.ItemCenter

How to anchor list items to the center-line of the viewport

autoCentering: AutoCenteringParams? = AutoCenteringParams()

AutoCenteringParams parameter to control whether space/padding should be automatically added to make sure that list items can be scrolled into the center of the viewport (based on their anchorType). If non-null then space will be added before the first list item, if needed, to ensure that items with indexes greater than or equal to the itemIndex (offset by itemOffset pixels) will be able to be scrolled to the center of the viewport. Similarly space will be added at the end of the list to ensure that items can be scrolled up to the center. If null no automatic space will be added and instead the developer can use contentPadding to manually arrange the items.

rotaryBehavior: RotaryBehavior? = RotaryDefaults.scrollBehavior(scrollableState = state)

Parameter for changing rotary behavior. Supports scroll RotaryDefaults.scrollBehavior and snap RotaryDefaults.snapBehavior. Note that when configuring fling or snap behavior, this rotaryBehavior parameter and the flingBehavior parameter that controls touch scroll are expected to produce similar list scrolling. For example, if rotaryBehavior is set for snap (using RotaryDefaults.snapBehavior), flingBehavior should be set for snap as well (using ScalingLazyColumnDefaults.snapFlingBehavior). Can be null if rotary support is not required.

content: ScalingLazyListScope.() -> Unit

The content of the ScalingLazyColumn

rememberScalingLazyListState

@Composable
fun rememberScalingLazyListState(
    initialCenterItemIndex: Int = 1,
    initialCenterItemScrollOffset: Int = 0
): ScalingLazyListState

Creates a ScalingLazyListState that is remembered across compositions.

Parameters
initialCenterItemIndex: Int = 1

the initial value for ScalingLazyListState.centerItemIndex, defaults to 1. This will place the 2nd list item (index == 1) in the center of the viewport and the first item (index == 0) before it.

initialCenterItemScrollOffset: Int = 0

the initial value for ScalingLazyListState.centerItemScrollOffset in pixels

Extension functions

inline fun <T : Any?> ScalingLazyListScope.items(
    items: Array<T>,
    noinline key: ((item) -> Any)? = null,
    crossinline itemContent: @Composable ScalingLazyListItemScope.(item) -> Unit
): Unit

Adds an array of items.

Parameters
items: Array<T>

the data array

noinline key: ((item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the list is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the list will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.

crossinline itemContent: @Composable ScalingLazyListItemScope.(item) -> Unit

the content displayed by a single item

inline fun <T : Any?> ScalingLazyListScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)? = null,
    crossinline itemContent: @Composable ScalingLazyListItemScope.(item) -> Unit
): Unit

Adds a list of items.

Parameters
items: List<T>

the data list

noinline key: ((item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the list is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the list will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.

crossinline itemContent: @Composable ScalingLazyListItemScope.(item) -> Unit

the content displayed by a single item

inline fun <T : Any?> ScalingLazyListScope.itemsIndexed(
    items: Array<T>,
    noinline key: ((index: Int, item) -> Any)? = null,
    crossinline itemContent: @Composable ScalingLazyListItemScope.(index: Int, item) -> Unit
): Unit

Adds an array of items where the content of an item is aware of its index.

Parameters
items: Array<T>

the data array

noinline key: ((index: Int, item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the list is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the list will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.

crossinline itemContent: @Composable ScalingLazyListItemScope.(index: Int, item) -> Unit

the content displayed by a single item

inline fun <T : Any?> ScalingLazyListScope.itemsIndexed(
    items: List<T>,
    noinline key: ((index: Int, item) -> Any)? = null,
    crossinline itemContent: @Composable ScalingLazyListItemScope.(index: Int, item) -> Unit
): Unit

Adds a list of items where the content of an item is aware of its index.

Parameters
items: List<T>

the data list

noinline key: ((index: Int, item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the list is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the list will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.

crossinline itemContent: @Composable ScalingLazyListItemScope.(index: Int, item) -> Unit

the content displayed by a single item