我一直在努力使它,使第一个项目上的一个LazyRow是居中的屏幕中间,只要打开应用程序。对齐行为在滚动时起作用,但当应用最初打开时,第一个项目不在页面的中心。
LazyRow支持snapper和无限滚动
@OptIn(ExperimentalSnapperApi::class)
@Composable
fun CircularList(
data: List<SingleBox>,
modifier: Modifier = Modifier,
isEndless: Boolean = true
) {
val listState = rememberLazyListState(
if (isEndless) Int.MAX_VALUE / 2 else 0
)
val configuration = LocalConfiguration.current
val screenWidth = configuration.screenWidthDp.dp
val contentPadding = PaddingValues(horizontal = screenWidth / 2) //This moves starting point of item horizontally
BoxWithConstraints {
LazyRow(
state = listState,
modifier = modifier,
contentPadding = contentPadding,
flingBehavior = rememberSnapperFlingBehavior(listState, SnapOffsets.Center, snapIndex = { _, startIndex, targetIndex ->
targetIndex.coerceIn(startIndex - 7, startIndex + 7) //This snaps item to center of page when LazyRow stops moving
})
)
{
items(
count = if (isEndless) Int.MAX_VALUE else data.size, //This makes it scroll infinitly
itemContent = {
val index = it % data.size
CustomItem(data[index]) // item composable
},
)
}
}
}
打开后的样子
打开应用程序时的外观
1条答案
按热度按时间aurhwmvo1#
我认为最好的选择是Accompanist Pager Library。
**更新:**此库已弃用,Compose 1.4.0+的androidx.compose.foundation.pager中提供了官方分页器支持