如何使用 Jetpack Compose 制作滚动条视图

问题描述 投票:0回答:1

我想要像这样的图片。我尝试了很多解决方案,但没有结果。

android scrollbar android-jetpack-compose
1个回答
5
投票

我使用此代码来显示页面已滚动了多少

BoxWithConstraints() {
            val scrollState = rememberScrollState()
            val viewMaxHeight = maxHeight.value
            val columnMaxScroll = scrollState.maxValue
            val scrollStateValue = scrollState.value
            val paddingSize = (scrollStateValue * viewMaxHeight) / columnMaxScroll
            val animation = animateDpAsState(targetValue = paddingSize.dp)
            val scrollBarHeight = viewMaxHeight / items

            Column(
                Modifier
                    .verticalScroll(state = scrollState)
                    .fillMaxWidth(),
                verticalArrangement = Arrangement.spacedBy(8.dp),
            ) {
               if (scrollStateValue < columnMaxScroll) {
                Box(
                    modifier = Modifier
                        .paddingFromBaseline(animation.value)
                        .padding(all = 4.dp)
                        .height(scrollBarHeight.dp)
                        .width(4.dp)
                        .background(
                            color = MaterialTheme.colors.primary.copy(alpha = ContentAlpha.disabled),
                            shape = MaterialTheme.shapes.medium
                        )
                        .align(Alignment.TopEnd),
                ) {}
            }
      }
}

结果:

1

© www.soinside.com 2019 - 2024. All rights reserved.