我有一个带有 2 个单元格的 LazyVerticalGrid。
LazyVerticalGrid(
cells = GridCells.Fixed(2),
content = {
items(moviePagingItems.itemCount) { index ->
val movie = moviePagingItems[index] ?: return@items
MovieItem(movie, Modifier.preferredHeight(320.dp))
}
renderLoading(moviePagingItems.loadState)
}
)
我正在尝试使用
LazyGridScope
显示全宽加载的fillParentMaxSize
修饰符。fun LazyGridScope.renderLoading(loadState: CombinedLoadStates) {
when {
loadState.refresh is LoadState.Loading -> {
item {
LoadingColumn("Fetching movies", Modifier.fillParentMaxSize())
}
}
loadState.append is LoadState.Loading -> {
item {
LoadingRow(title = "Fetching more movies")
}
}
}
}
但由于我们有 2 个单元格,加载可以占据屏幕的一半。像这样:有没有办法让我的加载 View 占据全宽?
最佳答案
喷气背包撰写 1.1.0-beta03
版本包括对 LazyVerticalGrid
的水平跨度支持.
这是示例用法:
private const val CELL_COUNT = 2
private val span: (LazyGridItemSpanScope) -> GridItemSpan = { GridItemSpan(CELL_COUNT) }
LazyVerticalGrid(
cells = GridCells.Fixed(CELL_COUNT),
content = {
items(moviePagingItems.itemCount) { index ->
val movie = moviePagingItems.peek(index) ?: return@items
Movie(movie)
}
renderLoading(moviePagingItems.loadState)
}
}
private fun LazyGridScope.renderLoading(loadState: CombinedLoadStates) {
if (loadState.append !is LoadState.Loading) return
item(span = span) {
val title = stringResource(R.string.fetching_more_movies)
LoadingRow(title = title)
}
}
此答案的代码示例可在以下位置找到:Jetflix/MoviesGrid.kt
关于android - Jetpack Compose 的 LazyVerticalGrid 是否有 span 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65981114/