致力于在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" />
答案 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>
}
}
}