How to calculate empty space in lazy column after last visible item
Asked Answered
B

3

6

How to calculate empty space in lazy column after last visible item in Jetpack compose.

enter image description here

Barranca answered 10/10, 2022 at 7:5 Comment(0)
M
4

You can use Modifier.onSizeChanged{intSize->} on your LazyColumn and its parent Composble and check the distance between them.

One thing to note here is it requires another recomposition after getting size of parent and list. So you might need do to a check or refer this answer to get dimensions without recomposition

How to get exact size without recomposition?

@Composable
private fun LazyColumnParentSizeSample() {

    var parentHeight by remember { mutableStateOf(0) }
    var listHeight by remember { mutableStateOf(0) }
    Column(modifier=Modifier.fillMaxSize()
        .onSizeChanged {
            parentHeight = it.height
        }) {

        LazyColumn(modifier=Modifier.onSizeChanged {
            listHeight = it.height
        }){
            items(10) {
                Text("Item: $it")
            }
        }
        Text("listHeight: $listHeight, parent height: $parentHeight, difference: ${parentHeight-listHeight}")

    }
}

To get value in dp you can use LocalDensity.current

    val diffInDp = LocalDensity.current.run {(parentHeight-listHeight).toDp()  }
Magisterial answered 10/10, 2022 at 7:8 Comment(4)
can you please add some sample code to understand betterBarranca
Added sample code with Modifier.onSizeChangedMagisterial
How to convert this height into DP ?Barranca
@TippuFisalSheriff updated answer. To convert to dp or convert from dp you can use LocalDensity.current.run{value conversion here}Magisterial
G
2

Use Modifier.onSizeChanged{} to get the size of the LazyColumn

Gleich answered 10/10, 2022 at 7:10 Comment(1)
can you please add some sample code to understand betterBarranca
D
2
LazyColumn(modifier = Modifier.onSizeChanged(size ->

int columnHeight = size.height
int screenHeight = //getyourscreenheighthere by using window class

int remainingSpacae = screenHeight - columnHeight

))

open this link to get screen height ->

Screen width and height in Jetpack Compose

Duncandunce answered 10/10, 2022 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.