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?

1
  • 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. Commented May 23, 2022 at 3:44

1 Answer 1

4

How about

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

To store all your Composables

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.