SnapFlingBehavior


A TargetedFlingBehavior that performs snapping of items to a given position.

You can use SnapLayoutInfoProvider.calculateApproachOffset to indicate that snapping should happen after this offset. If the velocity generated by the fling is high enough to get there, we'll use decayAnimationSpec to get to that offset and then we'll snap to the next bound calculated by SnapLayoutInfoProvider.calculateSnappingOffset using snapAnimationSpec.

If the velocity is not high enough, we'll use snapAnimationSpec to approach and the same spec to snap into place.

Please refer to the sample to learn how to use this API.

import androidx.compose.foundation.background
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

val state = rememberLazyListState()

LazyRow(
    modifier = Modifier.fillMaxSize(),
    verticalAlignment = Alignment.CenterVertically,
    state = state,
    flingBehavior = rememberSnapFlingBehavior(lazyListState = state)
) {
    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)
        }
    }
}
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.snapping.SnapLayoutInfoProvider
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

val state = rememberLazyListState()

// If you'd like to customize either the snap behavior or the layout provider
val snappingLayout = remember(state) { SnapLayoutInfoProvider(state) }
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)
        }
    }
}

Summary

Public constructors

SnapFlingBehavior(
    snapLayoutInfoProvider: SnapLayoutInfoProvider,
    decayAnimationSpec: DecayAnimationSpec<Float>,
    snapAnimationSpec: AnimationSpec<Float>
)
Cmn

Public functions

open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn
open suspend Float
ScrollScope.performFling(
    initialVelocity: Float,
    onRemainingDistanceUpdated: (Float) -> Unit
)

Perform a snapping fling animation with given velocity and suspend until fling has finished.

Cmn

Inherited functions

From androidx.compose.foundation.gestures.TargetedFlingBehavior
open suspend Float
ScrollScope.performFling(initialVelocity: Float)

Perform settling via fling animation with given velocity and suspend until fling has finished.

Cmn

Public constructors

SnapFlingBehavior

SnapFlingBehavior(
    snapLayoutInfoProvider: SnapLayoutInfoProvider,
    decayAnimationSpec: DecayAnimationSpec<Float>,
    snapAnimationSpec: AnimationSpec<Float>
)
Parameters
snapLayoutInfoProvider: SnapLayoutInfoProvider

The information about the layout being snapped.

decayAnimationSpec: DecayAnimationSpec<Float>

The animation spec used to approach the target offset. When the fling velocity is large enough. Large enough means large enough to naturally decay.

snapAnimationSpec: AnimationSpec<Float>

The animation spec used to finally snap to the correct bound.

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int
open suspend fun ScrollScope.performFling(
    initialVelocity: Float,
    onRemainingDistanceUpdated: (Float) -> Unit
): Float

Perform a snapping fling animation with given velocity and suspend until fling has finished. This will behave the same way as performFling except it will report on each remainingOffsetUpdate using the onRemainingDistanceUpdated lambda.

Parameters
initialVelocity: Float

velocity available for fling in the orientation specified in androidx.compose.foundation.gestures.scrollable that invoked this method.

onRemainingDistanceUpdated: (Float) -> Unit

a lambda that will be called anytime the distance to the settling offset is updated. The settling offset is the final offset where this fling will stop and may change depending on the snapping animation progression.

Returns
Float

remaining velocity after fling operation has ended