简体   繁体   English

我如何在项目中传递 2 个列表(LazyColumn)

[英]How I can pass 2 lists in items (LazyColumn)

I have LazyColumn and I want to pass 2 lists in items , how I can do it我有LazyColumn ,我想在items中传递 2 个列表,我该怎么做

I want to do something like that if it is possible如果可能的话,我想做这样的事情

LazyColumn(
    modifier = Modifier.fillMaxHeight(0.85f)
) {
    items(cartItems,products) { cartItems,product ->
        CardProduct2(cartItems, product)
    }
}

You can't.你不能。 But you can try to do the following:但是您可以尝试执行以下操作:

LazyColumn {
    items(max(cars.size, products.size)) { idx ->
        CardProduct(cars.getOrNull(idx), products.getOrNull(idx)) // Assume that your CardProduct composable are applying null values
    }
}

or better to merge this lists into one或者更好地将这些列表合并为一个

I'm assuming that if you are passing a list inside CardProduct is because you are using a LazyColumn inside to draw that list.我假设如果您在CardProduct中传递列表是因为您在内部使用LazyColumn来绘制该列表。

for that, just simply do this为此,只需这样做

Column {
    CardProduct2(cartItems, product)
}

Be aware of nesting LazyColumns , you will get a compilation error, it is better if you do the followin注意嵌套LazyColumns ,你会得到一个编译错误,如果你这样做会更好

LazyColumn(
    modifier = Modifier.fillMaxHeight(0.85f)
) {
    items(cartItems, null) { cartItems ->
        CardProduct2(cartItems)
    }

     items(null, product) { product ->
        CardProduct2(product)
    }
}

Inside CardProduct2 do a simple null check to draw either one of the two, or if you need the two together just create CardProduct3 that takes only product list.在 CardProduct2 内部做一个简单的 null 检查以绘制两者之一,或者如果您需要两者之一,只需创建仅包含产品列表的CardProduct3 But inside CardProduct should not be any LazyColumn code, it should only be the card details itself但是在 CardProduct 里面不应该是任何LazyColumn代码,它应该只是卡片详细信息本身

LazyColumn(
        modifier = Modifier.fillMaxHeight(0.85f)
    ) {
        items(cartItems) { cartItems ->
            CardProduct2(cartItems)
        }
    
         items(product) { product ->
            CardProduct3(product)
        }
    }

if you need cartItems and product data inside CardProduct2 simply create an object that take cartItems and product parameters and pass that only one as your data如果您需要cartItems的 cartItems 和product数据,只需创建一个CardProduct2即可获取cartItemsproduct参数,并将仅传递一个作为您的数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM