SnapPosition

Known direct subclasses
SnapPosition.Center

Aligns the center of the item with the center of the containing layout.

SnapPosition.End

Aligns the end of the item with the end of the containing layout.

SnapPosition.Start

Aligns the start of the item with the start of the containing layout.


Describes the snapping positioning (i.e. final positioning after snapping animation finishes) of a given snap item in its containing layout.

Summary

Nested types

Aligns the center of the item with the center of the containing layout.

Aligns the end of the item with the end of the containing layout.

Aligns the start of the item with the start of the containing layout.

Public functions

Int
position(
    layoutSize: Int,
    itemSize: Int,
    beforeContentPadding: Int,
    afterContentPadding: Int,
    itemIndex: Int,
    itemCount: Int
)

Calculates the snap position where items will be aligned to in a snapping container.

Cmn

Public functions

position

fun position(
    layoutSize: Int,
    itemSize: Int,
    beforeContentPadding: Int,
    afterContentPadding: Int,
    itemIndex: Int,
    itemCount: Int
): Int

Calculates the snap position where items will be aligned to in a snapping container. For instance, if SnapPosition.Center is used, once the snapping finishes the center of one of the items in the snapping container will be aligned exactly to the center of the snapping container, that is because the value returned by position was calculated as such a way that when one applies it to the item's current offset it will generate that final positioning.

The reference point is with respect to the start of the layout (including the content padding)

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.snapping.SnapLayoutInfoProvider
import androidx.compose.foundation.gestures.snapping.SnapPosition
import androidx.compose.foundation.gestures.snapping.rememberSnapFlingBehavior
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

val state = rememberLazyListState()
val density = LocalDensity.current

// Illustrate using a custom SnapPosition that will snap to a static location (200dp) after
// the content padding.
val snappingLayout =
    remember(state, density) {
        val snapPosition =
            object : SnapPosition {
                override fun position(
                    layoutSize: Int,
                    itemSize: Int,
                    beforeContentPadding: Int,
                    afterContentPadding: Int,
                    itemIndex: Int,
                    itemCount: Int
                ): Int {
                    return with(density) { beforeContentPadding + 200.dp.roundToPx() }
                }
            }
        SnapLayoutInfoProvider(state, snapPosition)
    }
val flingBehavior = rememberSnapFlingBehavior(snappingLayout)

LazyRow(
    modifier = Modifier.fillMaxSize(),
    verticalAlignment = Alignment.CenterVertically,
    state = state,
    flingBehavior = flingBehavior
) {
    items(200) {
        Box(
            modifier =
                Modifier.height(400.dp).width(200.dp).padding(8.dp).background(Color.Gray),
            contentAlignment = Alignment.Center
        ) {
            Text(it.toString(), fontSize = 32.sp)
        }
    }
}
Parameters
layoutSize: Int

The main axis layout size within which an item can be positioned.

itemSize: Int

The main axis size for the item being positioned within this snapping layout.

beforeContentPadding: Int

The content padding in pixels applied before this Layout's content.

afterContentPadding: Int

The content padding in pixels applied after this Layout's content.

itemIndex: Int

The index of the item being positioned.

itemCount: Int

The total amount of items in the snapping container.

Returns
Int

The offset of the snap position where items will be aligned to in a snapping container.