I am developing an android app using the Jetpack compose.
I should develop a Screen.
It has many Contents Card (Like Feed)
But it should be scrolled sticky. So I used the VerticalPager. When user scrolls, the contents should be scroll to only one by one.
The problem is that the first item is placed in the top of the ui. But from the second content onwards, it must be placed in the center of the screen like this:
I can use below code, and it looks like working fine.
val state = rememberPagerState()
VerticalPager(
state = state,
contentPadding = PaddingValues(
vertical = if (state.canScrollBackward) 120.dp else 0.dp
),
pageSize = PageSize.Fixed(328.dp),
) { idx ->
FeedUi(feeds[idx])
}
If I set the vertical contentPadding to 120.dp, the content is in centered.
So I use the state.canScrollBackward
.
state.canScrollBackward
means that current position is greater than 0. (1, 2, 3, ... not 0)
It looks like working. But It has two problem.
Is there any better solution?
As far as I understand your question, I think you can use:
flingBehavior = rememberSnapFlingBehavior(lazyListState = state)