skip to Main Content

I have LazyColumn and I want to pass 2 lists in items, how I can do it

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)
    }
}

2

Answers


  1. 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

    Login or Signup to reply.
  2. I’m assuming that if you are passing a list inside CardProduct is because you are using a LazyColumn inside to draw that list.

    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

    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. But inside CardProduct should not be any LazyColumn code, it should only be the card details itself

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search