[fenix] For https://github.com/mozilla-mobile/fenix/issues/21562 - Snap to next item when scrolling
This works by replacing the fling animation with snapping to the next item in the scroll direction.pull/600/head
parent
e7b2578a5a
commit
92bc5e93b1
@ -0,0 +1,47 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.compose
|
||||
|
||||
import androidx.compose.foundation.gestures.FlingBehavior
|
||||
import androidx.compose.foundation.gestures.ScrollScope
|
||||
import androidx.compose.foundation.lazy.LazyListState
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* [FlingBehavior] for a [LazyRow] that will automatically scroll the list in the fling direction
|
||||
* to fully show the next item.
|
||||
*/
|
||||
@Composable
|
||||
fun EagerFlingBehavior(
|
||||
lazyRowState: LazyListState
|
||||
): FlingBehavior {
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
return LazyListEagerFlingBehavior(lazyRowState, scope)
|
||||
}
|
||||
|
||||
private class LazyListEagerFlingBehavior(
|
||||
private val lazyRowState: LazyListState,
|
||||
private val scope: CoroutineScope
|
||||
) : FlingBehavior {
|
||||
override suspend fun ScrollScope.performFling(initialVelocity: Float): Float {
|
||||
val firstItemIndex = lazyRowState.firstVisibleItemIndex
|
||||
|
||||
val itemIndexToScrollTo = when (initialVelocity <= 0) {
|
||||
true -> firstItemIndex
|
||||
false -> firstItemIndex + 1
|
||||
}
|
||||
|
||||
scope.launch {
|
||||
lazyRowState.animateScrollToItem(itemIndexToScrollTo)
|
||||
}
|
||||
|
||||
return 0f // we've consumed the entire fling
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue