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

Summary

Public functions

open 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
open String
Cmn

Public functions

position

open 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.

toString

open fun toString(): String