【问题标题】:making a Column, Row setup that eventually "Exhausts" Jetpack Compose制作最终“排气”Jetpack Compose 的列、行设置
【发布时间】:2021-04-29 20:52:35
【问题描述】:

我想创建一个包含多行的 LazyColumn,每行最多包含 2 张“卡”,其中最后一行可能只包含一张卡(如果卡的数量是奇数)。我想建立一个系统来确定要创建的行数(卡数除以 2)然后根据卡数是奇数还是偶数,将行数四舍五入,然后放入 1 或 2 张卡行。

example:
5 cards = 3 total rows, 2 full rows, with the last row only having one card
4 cards = 2 total rows, 2 full rows
6 cards = 3 total rows, 3 full rows

“用尽”是指我希望系统在满足卡片数量后停止用卡片填充行。

    //Fix my code pls :)
fun isOddNum(num:Int):Boolean {
     return when {
     num % 2 == 0 ->false
     else->true}
    }


@Composable
fun myColumn(Amount_of_Cards:Int){
    val Amount_of_Rows = Amount_of_Cards/2
    var isOdd=isOddNum(Amount_of_Cards)
        LazyColumn() {
        items((0..Amount_of_Rows).toList()) {
            if (isOdd == true) {
                RestCardVert(type = "bagel", name = "BTB", isOpen =true , address = "219 Ba", rating = 4, color = 1 )

            } else {
                RestCardVert(type = "bagel", name = "BTB", isOpen =true , address = "219 Ba", rating = 4, color = 1 )
                RestCardVert(type = "bagel", name = "BTB", isOpen =true , address = "219 Ba", rating = 4, color = 1 )
                
                }
            }
        }
    }
}

【问题讨论】:

  • 你有什么问题?
  • 对不起,我问的是什么代码可以让我创建上面发布的列、行和卡片的配置,如果我的代码示例走在正确的轨道上,迭代我的下一步将是什么。

标签: android kotlin android-jetpack-compose


【解决方案1】:

我在这里回答了一个类似的问题:

What is the equivalent of [NestedScrollView + RecyclerView] or [Nested RecyclerView (Recycler inside another recycler) in Jetpack compose

这就是我所做的......

@Composable
fun MyComposable(wishlisted: List<String>) {
    LazyColumn(
        modifier = Modifier.fillMaxSize(),
    ) {
        // Turning the list in a list of lists of two elements each
        items(wishlisted.windowed(2, 2, true)) { sublist ->
            Row(Modifier.fillMaxWidth()) {
                sublist.forEach { item ->
                    Text(
                        item, modifier = Modifier
                            .height(120.dp)
                            .padding(4.dp)
                            .background(Color.Yellow)
                            .fillParentMaxWidth(.5f) // this is the trick
                    )
                }
            }
        }
    }
}

【讨论】:

  • 哇,谢谢.fillParentMaxWidth(.5f)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 2022-11-05
相关资源
最近更新 更多