如何使用 Jetpack Compose 创建 GridView [英] How to create GridView using Jetpack Compose

查看:140
本文介绍了如何使用 Jetpack Compose 创建 GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用回收器视图或 android.widget.gridview 的情况下在 Jetpack compose 中创建 Gridview?

解决方案

With 1.0.x

val numbers = (0..20).toList()懒惰垂直网格(单元格 = GridCells.Adaptive(minSize = 64.dp)){项目(数字){列(水平对齐 = Alignment.CenterHorizo​​ntally){文本(文本=数字")文本(文本 =$it",)}}}

cells = GridCells.Adaptive(minSize = 64.dp) 意味着将有尽可能多的列,每列至少为 64.dp,所有列将具有相等的宽度.

How to create Gridview in Jetpack compose without using recycler view or android.widget.gridview ?

解决方案

With 1.0.x the LazyVerticalGrid composable provides experimental support for displaying items in a grid.

val numbers = (0..20).toList()

LazyVerticalGrid(
    cells = GridCells.Fixed(4)
) {
    items(numbers.size) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(text = "Number")
            Text(text = "  $it",)
        }
    }
}

The cells = GridCells.Fixed(4) would mean that there are 4 columns 1/4 of the parent wide.

val numbers = (0..20).toList()

LazyVerticalGrid(
    cells = GridCells.Adaptive(minSize = 64.dp)
) {
    items(numbers) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(text = "Number")
            Text(text = "  $it",)
        }
    }
}

cells = GridCells.Adaptive(minSize = 64.dp) would mean that there will be as many columns as possible and every column will be at least 64.dp and all the columns will have equal width.

这篇关于如何使用 Jetpack Compose 创建 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆