NullPointerException
NullPointerException

Reputation: 37721

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

Upvotes: 2

Views: 71

Answers (2)

NullPointerException
NullPointerException

Reputation: 37721

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

Upvotes: 1

Nguyen Tien Dung
Nguyen Tien Dung

Reputation: 638

How about using Box() to wrap your ElementComposableFactory, something like this:

Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.YourBoxContentAlignment) {
     ElementComposableFactory(
        element = element,
        onClickAction = onClickAction,
        modifier = Modifier.align(Alignment.YourElementAlignment)
    )
}

Upvotes: 1

Related Questions