androidkotlinandroid-jetpack-composelazycolumnandroid-jetpack-compose-modifier

How to align specific children in a LazyColumn?


How can I align horizontally a particular children in a LazyColumn? it worked perfectly using Column, but it doesn't work in LazyColumn because I can't import Modifier.align for each particular item of the list, doesn't exist align import.

Column:

Column(
    modifier = modifier.fillMaxWidth(),
    horizontalAlignment = column.getHorizontalAligment()
) {
    for (element in column.childs) {
        ElementComposableFactory(
            element = element,
            onClickAction = onClickAction,
            modifier = Modifier.align(element.getHorizontalAligment())
        )
    }
}

LazyColumn:

LazyColumn(
    modifier = modifier.fillMaxWidth(),
    horizontalAlignment = column.getHorizontalAligment()
) {
    items(column.childs) { element ->
        ElementComposableFactory(
            element = element,
            onClickAction = onClickAction,
            modifier = Modifier.align(element.getHorizontalAligment()) <-- compilation error, align doesn't exist for the items of a lazycolumn
        )
    }
}

Solution

  • Well, finally I solved it using other approach:

    Modifier.fillParentMaxWidth()
         .wrapContentWidth(element.getHorizontalAligment())
    

    That makes the aligment work correctly

    LazyColumn(
        modifier = modifier.fillMaxWidth(),
        horizontalAlignment = column.getHorizontalAligment()
    ) {
        items(column.childs) { element ->
            ElementComposableFactory(
                element = element,
                onClickAction = onClickAction,
                modifier = Modifier.fillParentMaxWidth()
         .wrapContentWidth(element.getHorizontalAligment())
            )
        }
    }