15

Is there a way by which I can log an event when scrolling in a Column? I made it scrollable, saved the scroll state, but I can't find where to call a lambda function given as param to composable onScroll: () -> Unit

4 Answers 4

20

You can observe the scrollState:

 val scrollState = rememberScrollState() Column( modifier = Modifier .verticalScroll(scrollState) ) 

You can check the value of this scrollState:

if (scrollState.isScrollInProgress){ println("scrolling") } 

In case you need to wait for the scroll is completed, you can use if + DisposableEffect:

if (scrollState.isScrollInProgress) { DisposableEffect(Unit) { onDispose { println("scroll completed") } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

How does the DisposableEffect knows that it is for the the scrollState when we give it only Unit as a key?
@Henning When scroll is finished, DisposableEffect disappears from the composition and gets disposed automatically. Compose is magic!
11

You can keep listening the state of your lazy list by collecting the value like this:

val state = rememberLazyListState() LaunchedEffect(state) { snapshotFlow { state.isScrollInProgress } .collect { /* here boolean result */ } } 

Comments

5

If you wondering how to do this in a LazyColumn:

val nestedScrollConnection = remember { object : NestedScrollConnection { override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity { // On scroll ended detection println("scroll completed") return super.onPostFling(consumed, available) } } } 

and

LazyColumn( modifier = Modifier.nestedScroll(nestedScrollConnection) ) 

Comments

2

There's a property on scroll state called isScrollInProgress. That one can be used. If isScrollInProgress is true i called my own lambda function onScroll(). Works fine.

1 Comment

Can you write some code to explain what you mean?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.