Search code examples
kotlinandroid-jetpack-composelazycolumnandroid-jetpack-compose-lazy-column

jetpack compose : creating LazyColumn with items parameter


LazyColumn(modifier = Modifier.fillMaxWidth(), state = listState) {
    //ABC Category Items
    item {
        ABC(componentCoordinator = componentCoordinator)
    }

    //DEF Category Items
    item {
        DEF(coordinator = screenCoordinator)
    }

    //IJK Category Items
    item {
        IJK()
    }

    //XYZ Category Items
    item {
        XYZ(coordinator = screenCoordinator)
    }
}

@Composable
fun ABC(
    viewModel: ViewModel = hiltViewModel(),
    componentCoordinator: ComponentCoordinator
) {
    LazyRow(
        modifier = Modifier
            .fillMaxWidth()
            .height(64.dp),
        horizontalArrangement = Arrangement.SpaceEvenly,
    ) {
.........
})


All ABC, DEF, IJK, XYZ are Composable methods with Column, Row, LazyRow combinations.

I have to make them all inside individual item {} in order to jump them separately on basis of their index (using listState.animateScrollToItem(int)). Now, the better way of creating this LazyColumn is with "items" (instead of "item") parameter with these Composible functions list.

something like this:

LazyColumn(
        modifier = Modifier.fillMaxWidth(), state = listState
    ) {

        items(myItems, key = { it.uri }) { item ->
....
})

What could be the Array initialization code (as these methods dont have same parameters) for this and the LazyColumn function body?


Solution

  • How about

    var itemsList = mutableStateListOf<@Composable (vararg : Any) -> Unit>)
    

    To store all your Composables