简体   繁体   English

如何在 jetpack compose 中向 LazyVerticalGrid 添加一个 stickyHeader,例如 LazyColumn?

[英]How can I add a stickyHeader to LazyVerticalGrid like LazyColumn in jetpack compose?

I want to achieve a UI effect like this:我想实现这样的UI效果:

<GridView>
    <Title of Grid content in a single row />
    <Grid content arranged in the form of n * 3 />

    <Title of Grid content in a single row />
    <Grid content arranged in the form of n * 3 />

    <Title of Grid content in a single row />
    <Grid content arranged in the form of n * 3 />
</GridView>

But I am not able to add the row to the LazyColumnGrid.但我无法将该行添加到 LazyColumnGrid。

I tried using the LazyColumnGrid around Column with vertical scroll, but it says it's wrong to nest two scrollable views in the same direction.我尝试使用带有垂直滚动的列周围的 LazyColumnGrid,但它说it's wrong to nest two scrollable views in the same direction.

Also, I tried to use item{ } on <Title of Grid /> , but that made item{ } an item in the gridview, not a single row.此外,我尝试在<Title of Grid />上使用item{ } ,但这使得item{ }成为 gridview 中的项目,而不是单行。

So how can I achieve this in a simple way?那么我怎样才能以简单的方式实现这一目标呢?

You can't really create a sticky header with Compose's LazyGrid apis, but you can create a header as follow:您不能真正使用 Compose 的 LazyGrid api 创建一个粘性 header,但您可以创建一个 header,如下所示:


fun LazyGridScope.header(
    content: @Composable LazyGridItemScope.() -> Unit
) {
    item(span = { GridItemSpan(this.maxLineSpan) }, content = content)
}

The code uses the span block with this.maxLineSpan which becomes a full-width header.该代码使用带有this.maxLineSpan的 span 块,它变成了全宽 header。

So, you can use it like this所以,你可以像这样使用它

LazyVerticalGrid(
    ...
) {
    header {
        Text("Header Title") // or any composable for your single row
    }
    items(count = n * 3) {
        GridItem() 
    }


    header {
        Text("Header Title") // or any composable for your single row
    }
    items(count = n * 3) {
        GridItem() 
    }
    
}

BONUS You can also offset your cells this way奖金你也可以通过这种方式抵消你的细胞

fun LazyGridScope.offSetCells(count: Int) {
    item(span = { GridItemSpan(count) }) {}
}

...

LazyVerticalGrid(
   ...
) {
    offsetCells(count = 3)
}

The currently available way is to implement it without the nested scrolling at all.当前可用的方法是在没有nested scrolling情况下实现它。

LazyColumn {
    grouped.forEach { (headText, allGrids) ->
        stickyHeader {
            //Sticky Heading
            Text(text = "Starting with $headText");
        }
        items(allGrids) { grid ->
            // Implement <Grid content- n*3 /> without scrolling

            // Method 1: Rows & .forEach{ }
            Row {
             name.forEach { gridItem ->
               // implement gridItem
               Text(
                 gridItem.toString(),
                 modifier = Modifier
                   .border(BorderStroke(2.dp, Color.LightGray))
                   .padding(15.dp)
                   .fillParentMaxWidth(0.3f)
                )
               }
             }

            // Method 2: Using basic Column & Rows
        }
    }
}

Output输出

标题网格布局

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

相关问题 Android Jetpack Compose中如何结合Paging使用LazyColumn stickyHeader? - How to use LazyColumn stickyHeader in combination with Paging in Android Jetpack Compose? 如何在 Jetpack Compose 的 LazyColumn 中使用 LazyColumn? - How can I use LazyColumn in LazyColumn in jetpack compose? 如何在 jetpack compose 的 LazyColumn 中添加项目? - How to add item in LazyColumn in jetpack compose? Jetpack Compose:如何监听 lazycolumn 等列当前可见的项目? - Jetpack Compose: How to listen column currently visible items like lazycolumn? 如何使用带有 LazyVerticalGrid 的 jetpack 组合分页 - How to use jetpack compose paging with LazyVerticalGrid 如何在 LazyColumn Jetpack Compose 中的项目之间添加分隔符? - How to add dividers between items in a LazyColumn Jetpack Compose? Jetpack Compose:如何在 LazyVerticalGrid 中将项目居中? - Jetpack Compose: How to center an item in LazyVerticalGrid? 如何将资产文件夹中的本地 JSON 文件解析为 Jetpack Compose LazyColumn? - How can I parse a local JSON file from assets folder into a Jetpack Compose LazyColumn? Jetpack Compose LazyVerticalGrid 口吃 - Jetpack Compose LazyVerticalGrid stuttering Jetpack Compose LazyColumn 项目在 stickyHeader 上滚动并且不滚动到最后一个项目 - Jetpack Compose LazyColumn items scroll over stickyHeader and does not scroll to last item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM