I'm trying to create a `SnapLayoutInfoProvider` fo...
# compose
s
I'm trying to create a
SnapLayoutInfoProvider
for a
ScrollState
(used by a
Pager
composable, where all the items have
fillMaxSize
), but I don't really get what
calculateSnappingOffsetBounds
is expected to return. I don't understand the doc either. I mean, shouldn't an offset be enough (i.e. the new `ScrollState`'s
value
)?
1
j
@levima
s
I think I got it:
Copy code
@ExperimentalFoundationApi
fun SnapLayoutInfoProvider(
    itemCount: Int,
    scrollState: ScrollState,
): SnapLayoutInfoProvider = object : SnapLayoutInfoProvider {
    override fun Density.calculateApproachOffset(initialVelocity: Float): Float = 0f

    override fun Density.calculateSnappingOffsetBounds(): ClosedFloatingPointRange<Float> {
        val bound0 = -scrollState.value % snapStepSize()
        val bound1 = snapStepSize() + bound0

        return (if (bound0 >= 0 && bound1 < 0) bound1.rangeTo(bound0) else bound0.rangeTo(bound1))
    }

    override fun Density.snapStepSize(): Float = scrollState.maxValue.toFloat() / (itemCount - 1)
}
l
Thanks for pointing this out, I'll try to improve the documentation. There's a demo that uses Scroll state for snapping https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]mos/snapping/RowSnapLayoutInfoProvider.kt;l=1?q=RowSnapLa&amp;sq= But it looks like you got the idea. for Scroll state the scroll value only provides part of the information necessary to determine the lower and upper bounds for snapping.
s
Thanks!
600 Views