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

Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
  • 1
    You could create a enum/sealed class (in case of different content data) for each item type, and pass array of items, but I'd say that this sugar doesn't give you much readability, unless you need to change order dynamically. – Phil Dukhov May 23 '22 at 03:44

1 Answers1

4

How about

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

To store all your Composables

Richard Onslow Roper
  • 5,477
  • 2
  • 11
  • 42