I want to create a timepicker in jetpack Compose like this:
To show the values I am using a lazyList, but I cannot find an elegant method to show only 3 elements at a time better than: LazyColumn (modifier = Modifier.height(x.dp))
I want to create a timepicker in jetpack Compose like this:
To show the values I am using a lazyList, but I cannot find an elegant method to show only 3 elements at a time better than: LazyColumn (modifier = Modifier.height(x.dp))
On
I had the exact same problem and solved it like this:
@Composable
fun LimitedLazyColumn(items : List<String>, limit : Int) {
val itemHeightPixels = remember { mutableStateOf(0) }
LazyColumn(
modifier = Modifier.height(pixelsToDp(pixels = itemHeightPixels.value * limit))
) {
items(items) { item ->
Text(
text = item,
modifier = Modifier.onSizeChanged { size -> itemHeightPixels.value = size.height }
)
}
}
}
@Composable
private fun pixelsToDp(pixels: Int) = with(LocalDensity.current) { pixels.toDp() }
You can show the element you want with color alpha with list index;