I was wondering if there is a way or a resource I could refer to in order to achieve a side effect on a LazyRow
when an item is scrolled?
The side effect is basically to call a function in the viewModel to alter the state of the list's state.
- The side effect should be only executed only if the current firstVisibleItemIndex after
- scroll is different than before
The side effect should not be executed the item is not fully scrolled
I am implementing a fullscreen
LazyRow
items with a snap behavior
So far I have tried NestedScrollConnection
class OnMoodItemScrolled : NestedScrollConnection {
override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
viewModel.fetchItems()
return super.onPostFling(consumed, available)
}
}
The issue with the above is that the side effect is going to be executed anyway even-though the item displayed after the scroll is the same as before the scroll.
I also tried to collecting the listState interaction as the following
val firstVisibleItem: Int = remember { sectionItemListState.firstVisibleItemIndex }
sectionItemListState.interactionSource.collectIsDraggedAsState().let {
if (firstVisibleItem != sectionItemListState.firstVisibleItemIndex) {
viewModel.fetchItems()
}
}
The issue with the above is that the side effect is going to be executed the second the composable is composed for the first time.