简体   繁体   English

Jetpack Compose 的 LazyVerticalGrid 是否有跨度策略

[英]Does Jetpack Compose's LazyVerticalGrid have span strategy

I have a LazyVerticalGrid with 2 cells.我有一个带有 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)
    }
)

在此处输入图像描述

I am trying to show full width loading with LazyGridScope 's fillParentMaxSize modifier.我正在尝试使用LazyGridScopefillParentMaxSize修饰符显示全宽加载。

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")
        }
    }
  }
}

But since we have 2 cells, the loading can occupy half of the screen.但是因为我们有 2 个单元格,加载可以占据屏幕的一半。 Like this:像这样:

在此处输入图像描述

Is there a way my loading view can occupy full width?有没有办法让我的加载视图占据全宽?

LazyVerticalGrid has a span strategy built into items() and itemsIndexed() LazyVerticalGriditems()itemsIndexed()中内置了跨度策略

@Composable
fun SpanLazyVerticalGrid(cols: Int, itemList: List<String>) {
    val lazyGridState = rememberLazyGridState()
    LazyVerticalGrid(
            columns = GridCells.Fixed(cols),
            state = lazyGridState
    ) {
        items(itemList, span = { item ->
            val lowercase = item.lowercase()
            val span = if (lowercase.startsWith("a") || lowercase.lowercase().startsWith("b") || lowercase.lowercase().startsWith("d")) {
                cols
            }
            else {
                1
            }
            GridItemSpan(span)
        }) { item ->
            Box(modifier = Modifier
                    .fillMaxWidth()
                    .height(150.dp)
                    .padding(10.dp)
                    .background(Color.Black)
                    .padding(2.dp)
                    .background(Color.White)
            ) {
                Text(
                        modifier = Modifier.align(Alignment.Center),
                        text = item,
                        fontSize = 18.sp
                )
            }
        }
    }
}

' '

val names = listOf("Alice", "Bob", "Cindy", "Doug", "Ernie", "Fred", "George", "Harris")
SpanLazyVerticalGrid(
        cols = 3,
        itemList = names
)

在此处输入图像描述

Try something like:尝试类似:

var cellState by remember { mutableStateOf(2) }
LazyVerticalGrid(
    cells = GridCells.Fixed(cellState),
    content = {
        items(moviePagingItems.itemCount) { index ->
            val movie = moviePagingItems[index] ?: return@items
            MovieItem(movie, Modifier.preferredHeight(320.dp))
        }
        renderLoading(moviePagingItems.loadState) {
            cellState = it
        }
    }
)

The renderLoading function: renderLoading function:

fun LazyGridScope.renderLoading(loadState: CombinedLoadStates, span: (Int) -> Unit) {
    when {
        loadState.refresh is LoadState.Loading -> {
            item {
                LoadingColumn("Fetching movies", Modifier.fillParentMaxSize())
            }
            span(1)
        }
        ...
        else -> span(2)
    }
  
}

I have created an issue for it: https://issuetracker.google.com/u/1/issues/176758183我为它创建了一个问题: https://issuetracker.google.com/u/1/issues/176758183

Current workaround I have is to use LazyColumn and implement items or header.我目前的解决方法是使用LazyColumn并实现项目或 header。

override val content: @Composable () -> Unit = {
    LazyColumn(
            contentPadding = PaddingValues(8.dp),
            content = {
                items(colors.chunked(3), itemContent = {

                    Row(horizontalArrangement = Arrangement.SpaceEvenly) {
                        val modifier = Modifier.weight(1f)
                        it.forEach {
                            ColorItem(modifier, it)
                        }
                        for (i in 1..(3 - it.size)) {
                            Spacer(modifier)
                        }
                    }
                })
                item {
                    Text(
                            text = stringResource(R.string.themed_colors),
                            style = MaterialTheme.typography.h3
                    )
                }
                items(themedColors.chunked(3), itemContent = {
                    Row(horizontalArrangement = Arrangement.SpaceEvenly) {
                        val modifier = Modifier.weight(1f)
                        it.forEach {
                            ColorItem(modifier, it)
                        }
                        for (i in 1..(3 - it.size)) {
                            Spacer(modifier)
                        }
                    }
                })
            })
}

Jetpack Compose 1.1.0-beta03 version includes horizontal span support for LazyVerticalGrid . Jetpack Compose 1.1.0-beta03版本包括对LazyVerticalGrid的水平跨度支持。

Here is the example usage:这是示例用法:

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)
    }
}

Code examples of this answer can be found at: Jetflix/MoviesGrid.kt可以在以下位置找到此答案的代码示例: Jetflix/MoviesGrid.kt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetpack Compose LazyVerticalGrid 口吃 - Jetpack Compose LazyVerticalGrid stuttering LazyVerticalGrid 用于在 jetpack compose 中分页项目 - LazyVerticalGrid for paging items in jetpack compose 如何使用设置的全跨度或动态 GridCells 数来实现 LazyVerticalGrid。在 jetpack compose 中固定? - How to impl LazyVerticalGrid with set full span or dynamic num of GridCells.Fixed in jetpack compose? 如何使用带有 LazyVerticalGrid 的 jetpack 组合分页 - How to use jetpack compose paging with LazyVerticalGrid Jetpack Compose:如何在 LazyVerticalGrid 中将项目居中? - Jetpack Compose: How to center an item in LazyVerticalGrid? 如何在 jetpack compose 中使用 lazy column 和 lazyverticalgrid? - How can I use lazy column and lazyverticalgrid in jetpack compose? Jetpack 组合 LazyVerticalGrid 项目抛出 java.lang.IllegalStateException - Jetpack compose LazyVerticalGrid items throws java.lang.IllegalStateException Jetpack Compose:如何将 LazyVerticalGrid 放入可滚动列中? - Jetpack Compose: How to put a LazyVerticalGrid inside a scrollable Column? 如何在 jetpack compose 中向 LazyVerticalGrid 添加一个 stickyHeader,例如 LazyColumn? - How can I add a stickyHeader to LazyVerticalGrid like LazyColumn in jetpack compose? 在 Jetpack Compose 中使用 HorizontalPager + LazyVerticalGrid 时性能不佳 - Performance is bad when using HorizontalPager + LazyVerticalGrid in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM