Compose横向列表和网格列表

横向列表LazyRow

LazyRow和LazyColumn使用类似。

/**
 * 横向列表LazyRow
 */
@Composable
fun LazyRowTest() {
    val context = LocalContext.current
    val dataList = arrayListOf<Int>()
    for (index in 1..50) {
        dataList.add(index)
    }

    LazyRow {
        items(dataList) { data ->
            Box(
                modifier = Modifier
                    .fillMaxHeight()
                    .padding(start = 1.dp)
                    .background(Color.White)
                    .clickable {
                        Toast
                            .makeText(context, "$data", Toast.LENGTH_SHORT)
                            .show()
                    }
                    .padding(10.dp),
            ) {
                Text(
                    text = "第${data}条数据",
                    modifier = Modifier.align(Alignment.Center), // 设置上下居中
                )
            }
        }
    }
}

垂直网格布局LazyVerticalGrid

垂直网格布局使用LazyVerticalGrid组件,GridCells.Adaptive以最小宽度设置横向排列数量,GridCells.Fixed直接设置横向排列数量。

/**
 * 垂直网格布局(纵向滑动)
 */
@Composable
fun LazyVerticalGridTest() {
    val photos = arrayListOf<Int>()
    for (index in 0..50) {
        photos.add(R.drawable.img)
    }

    LazyVerticalGrid(
        //以最小宽度设置横向排列数量
//        columns = GridCells.Adaptive(minSize = 120.dp)
        //直接设置横向排列数量
        columns = GridCells.Fixed(count = 3)
    ) {
        items(photos) { photo ->
            Image(
                painter = painterResource(photo),
                "",
                modifier = Modifier.padding(2.dp)
            )
        }
    }
}

水平网格布局LazyHorizontalGrid

 水平网格布局和垂直网格布局类似,只是纵向滑动变成了横向滑动,属性值和垂直网格布局相反。

/**
 * 水平网格布局(横向滑动)
 */
@Composable
fun LazyHorizontalGridTest() {
    val photos = arrayListOf<Int>()
    for (index in 0..50) {
        photos.add(R.drawable.img)
    }

    LazyHorizontalGrid(
        //以最小大小设置纵向排列数量
        rows = GridCells.Adaptive(minSize = 120.dp)
        //设置纵向排列数量
//        rows = GridCells.Fixed(count = 6)
    ) {
        items(photos) { photo ->
            Image(
                painter = painterResource(photo),
                "",
                modifier = Modifier.padding(5.dp)
            )
        }
    }
}

在计算机科学中,Compose 弧形列表是一种数据构,用于表示一系列的函数组合。它是函数式编程中的一个重要概念。 Compose 弧形列表由多个函数组成,每个函数都接受一个参数并返回一个结果。这些函数按照顺序依次应用于输入值,每个函数的输出作为下一个函数的输入。最终的结果是将所有函数依次应用于输入值后得到的输出。 Compose 弧形列表的优点在于它可以将多个简单的函数组合成一个复杂的函数,提高代码的可读性和可维护性。通过使用Compose 弧形列表,可以将函数的组合过程清晰地表达出来,而不需要嵌套多层函数调用。 以下是Compose 弧形列表的示例代码: ```python def compose(*functions): def inner(arg): result = arg for f in reversed(functions): result = f(result) return result return inner # 定义一些简单的函数 def add_one(x): return x + 1 def multiply_by_two(x): return x * 2 def square(x): return x ** 2 # 创建一个Compose 弧形列表 composed_function = compose(square, multiply_by_two, add_one) # 应用Compose 弧形列表 result = composed_function(3) print(result) # 输出:64 ``` 在上面的示例中,我们定义了三个简单的函数:add_one、multiply_by_two 和 square。然后,我们使用compose函数将它们组合成一个Compose 弧形列表,并将这个Compose 弧形列表应用于输入值3。最终的结果是64,这是将3依次经过add_one、multiply_by_two 和 square三个函数得到的输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

举儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值