I am making a calendar with the help of a lazyRow. I now have the problem that I want the row to snap to the index after a certain scroll amount so it shouldn't be possible to be stuck in between indexes. Is there a way to do that?
LazyRow(state = calendarViewModel.listState, modifier = Modifier.fillMaxWidth()) {
calendarYears.forEach {
items(it.months.count()) { index ->
calendarViewModel.onEvent(CalendarEvent.ClickedMenuItem(index))
CalendarRowItem(
modifier = Modifier.fillParentMaxWidth(),
calendarSize = it.months[index].amountOfDays,
initWeekday = it.months[index].startDayOfMonth.ordinal,
textColor = MaterialTheme.colors.secondaryVariant,
clickedColor = MaterialTheme.colors.primary,
textStyle = MaterialTheme.typography.body1
)
}
}
}
You can use the HorizontalPager
from accompanist library which provides this fling behavior out-of-the-box and it uses LazyRow
internally.
Another option could be use the Snapper library created by @chris-banes
Add the dependency in your build.gradle
.
dependencies {
implementation "dev.chrisbanes.snapper:snapper:<version>"
}
and use it in your LazyRow
.
val lazyListState = rememberLazyListState()
LazyRow(
state = lazyListState,
flingBehavior = rememberSnapperFlingBehavior(lazyListState),
) {
// content
}
Result: