如何在VerticalScroller(Android开发人员撰写)中显示指标?

时间:2019-11-12 11:06:46

标签: android scrollbar android-jetpack-compose

致力于在Android Studio 4.0 Canary中理解android jetpackcompose

我有一个jetpackcompose的简单VerticalScroller设置,如下所示:

@Composable
fun MyScreenContent(appState: AppState = AppState())
{

    FlexColumn{


       flexible(flex = 1f){
           VerticalScroller {
             Column {
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
                CardDemo()
            }
        }
    }
 }
}

它可以完美滚动,但是我想要一个scrollindicator,它向我显示列表滚动了多远,如何打开列表,找不到任何要包含的ScrollIndicator(即)组件。

参考到RecyclerView可见的不可组合设置(xml)中移至scrollindicator

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/station_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/recyclerview_background_color"
                android:clipChildren="true"
                android:clipToPadding="false"
                android:fadeScrollbars="false"
                android:paddingLeft="0dp"
                android:paddingRight="0dp"
                android:scrollbarSize="4dp"
                android:scrollbarThumbVertical="@color/colorPrimary"   <---
                android:scrollbars="vertical"                          <---
                android:visibility="visible"
                app:isGone="@{!hasStations}"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                app:layout_constraintStart_toStartOf="parent"
                tools:context="no.rogo.projectfk303.ui.activities.MainActivity"
                tools:listitem="@layout/list_item_station" />

1 个答案:

答案 0 :(得分:0)

对于jetpack compose桌面,它会是:

@Composable
fun ScrollBox(
    modifier: Modifier = Modifier,
    columnVerticalArrangement: Arrangement.Vertical = Arrangement.Top,
    columnHorizontalAlignment: Alignment.Horizontal = Alignment.Start,
    rowHorizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    rowVerticalAlignment: Alignment.Vertical = Alignment.Top,
    content: @Composable ColumnScope.() -> Unit
) {
    val horizontalScrollState = rememberScrollState(0)
    val verticalScrollState = rememberScrollState(0)

    Box(modifier) {
        Row(Modifier.horizontalScroll(horizontalScrollState), horizontalArrangement = rowHorizontalArrangement, verticalAlignment = rowVerticalAlignment) {
            Column(Modifier.verticalScroll(verticalScrollState), columnVerticalArrangement, columnHorizontalAlignment) {
                content()
            }
        }
        HorizontalScrollbar(
            adapter = rememberScrollbarAdapter(horizontalScrollState),
            modifier = Modifier.align(Alignment.BottomEnd),
            style = LocalScrollbarStyle.current.copy(unhoverColor = LocalScrollbarStyle.current.hoverColor.copy(alpha = 0.35f))
        )
        VerticalScrollbar(
            adapter = rememberScrollbarAdapter(verticalScrollState),
            modifier = Modifier.align(Alignment.CenterEnd),
            style = LocalScrollbarStyle.current.copy(unhoverColor = LocalScrollbarStyle.current.hoverColor.copy(alpha = 0.35f))
        )
    }
}

和用法:

    BoxWithConstraints {
        val parentHeight: Dp = with(LocalDensity.current)  { constraints.maxHeight.toDp() }

        Box(
            Modifier.background(Color.Gray)
                .sizeIn(
                    minHeight = 50.dp,
                    maxHeight = parentHeight * 0.5f
                )
        ) {
            ScrollBox {
                <your large Composables here>
            }
        }
    }