'OnScrolled in LazyColumnFor ( Jetpack Compose )
Is anyone could find an equivalent of onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) in LazyColumnFor, thank you
Solution 1:[1]
You can get the position of a first item by adding an onGloballyPositioned https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/OnGloballyPositionedModifier option to the first item's modifier.
It returns:
- an initial position after layout creation
- an updated position every time during a scroll event
The LayoutCoordinates object inside the lambda let you get:
positionInParent()positionInRoot()positionInWindow()
To get an x or y from the LazyColumn/LazyRow you can use this part of code:
val initialLayoutY: MutableState<Float?> = rememberSaveable { mutableStateOf(null) }
Text(
text = "Lorem",
modifier = Modifier.onGloballyPositioned { layoutCoordinates ->
if (initialLayoutY.value == null) {
initialLayoutY.value = layoutCoordinates.positionInRoot().y
}
val currentTextLayoutY: Float = layoutCoordinates.positionInRoot().y
val currentLazyListY: Float = initialLayoutY.value!! - currentTextLayoutY
Log.e("Lazy Column y", currentLazyListY.toString())
}
)
Unfortunately, it skips a lot of values during fast scrolling. Here is a result:
Solution 2:[2]
It is not exactly onScrolled.
With 1.0.0-beta02 you can use LazyListState#isScrollInProgress to check if the list is currently scrolling by gesture, fling or programmatically or not.
val itemsList = (0..55).toList()
val state = rememberLazyListState()
if (state.isScrollInProgress){
//....
}
LazyColumn(state = state) {
items(itemsList) {
Text(".....")
}
}
Solution 3:[3]
with 1.0.0-alpha12
It should be what you want:
val scrollCallback = object : ScrollCallback {
override fun onCancel() {
super.onCancel()
}
override fun onScroll(scrollDistance: Float): Float {
return super.onScroll(scrollDistance)
}
override fun onStart(downPosition: Offset) {
super.onStart(downPosition)
}
override fun onStop(velocity: Float) {
super.onStop(velocity)
}
}
LazyColumnFor(
modifier = Modifier.scrollGestureFilter(scrollCallback, Orientation.Vertical),
items = listOf("")
) { item ->
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Patryk Kubiak |
| Solution 2 | Gabriele Mariotti |
| Solution 3 |

