masdea4
masdea4

Reputation: 89

making a Column, Row setup that eventually "Exhausts" Jetpack Compose

I want to create a LazyColumn, that contains multiple Rows, which contains a maximum of 2 "Cards" each, where the final Row may only contain one card (if amount of cards is odd). I want to build a system which determines the amount of rows to create (Amount of cards divided by 2) then depending on if the Amount of cards is odd or even, to round up the number of rows, then put 1 or 2 cards in the Row.

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

By "exhaust" I mean that I want the system to stop filling Rows with cards after the number of cards has been satisfied.

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

Upvotes: 1

Views: 1477

Answers (1)

nglauber
nglauber

Reputation: 23964

I answered a similar question here:

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

This is what I did...

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

Upvotes: 2

Related Questions