android - Jetpack Compose 的 LazyVerticalGrid 是否有 span 策略

标签 android android-jetpack-compose

我有一个带有 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)
    }
)
enter image description here
我正在尝试使用 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 个单元格,加载可以占据屏幕的一半。像这样:
enter image description here
有没有办法让我的加载 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/

相关文章:

android - 服务器托管仅允许授权 IP 访问数据库

android - Jetpack Compose 中 BasicTextField 的焦点有多清晰?

android - 文本字段 - LazyColum jetpack compose 中的文本位于 IME 下方

android - 如何在 Jetpack Compose 中返回值

android - 如何在jetpack compose中删除文本基线下方的空格?

android - 如何在android中实现 Material 设计圆形进度条

android - 使用 Jetpack Compose Navigation 时导航图是否已过时?

python - 从 Android 客户端调用在 Azure VM 中运行的 python 函数

android - 将富编辑文本保存到数据库

android - 如何使用带有 LazyVerticalGrid 的 jetpack 组合分页