我想在jetpack Compose中创建一个时间选择器,如下所示:
显示值 我正在使用lazyList,但我找不到一个优雅的方法来一次只显示3个元素,而不是:LazyColumn (modifier = Modifier.height(x.dp))
LazyColumn (modifier = Modifier.height(x.dp))
sigwle7e1#
你可以用带列表索引的alpha颜色显示你想要的元素;
@Composable fun ShowOnlySomeElementsInLazyColumn( items: List<String> ) { val lazyListState = rememberLazyListState() val firstVisibleIndex = lazyListState.firstVisibleItemIndex val centerIndex = firstVisibleIndex + 2 LazyColumn( state = lazyListState, modifier = Modifier .fillMaxWidth() .height(100.dp), horizontalAlignment = Alignment.CenterHorizontally ) { items(items.size){index -> when(centerIndex){ index -> { Text(text = items[index]) } index + 1 -> { Text(text = items[index]) } index - 1-> { Text(text = items[index]) } else -> { Text( text = items[index], color = LocalContentColor.current.copy(alpha = 0f) ) } } } } }
jv4diomz2#
我也遇到了同样的问题,我是这样解决的:
@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() }
2条答案
按热度按时间sigwle7e1#
你可以用带列表索引的alpha颜色显示你想要的元素;
jv4diomz2#
我也遇到了同样的问题,我是这样解决的: