如下面的代码所示,我有一个回调,我想收集VerticalScroller
' 的位置,但如果我这样做,VerticalScrollers 小部件将不再滚动,因为在源代码中VerticalScroller
,初始回调调用VerticalScroller
's scrollerPosition
。从我想使用它的回调中无法访问此值。有没有一种快速的方法来调用scrollerPosition
或使滚动行为继续?(不创建递归“竞争”条件?)
@Composable
fun StationsScreen(deviceLocation: LocationData, openDrawer: () -> Unit)
{
var scrollPosition = ScrollPosition(0.px,0.px)
FlexColumn {
inflexible {
TopAppBar(
title = {Text(text = "Stations")},
navigationIcon = {
VectorImageButton(id = R.drawable.ic_baseline_menu_24) {
openDrawer()
}
}
)
}
inflexible {
Column (
mainAxisSize = LayoutSize.Expand,
crossAxisSize = LayoutSize.Expand
){
LocationWidget(deviceLocation)
}
}
inflexible {
Column(
mainAxisSize = LayoutSize.Expand,
crossAxisSize = LayoutSize.Expand
){
PushWidget(){
deviceLocation.lat++
deviceLocation.lng++
}
}
}
inflexible{
Column(
mainAxisSize = LayoutSize.Expand,
crossAxisSize = LayoutSize.Expand
) {
ScrollPosWidget(scrollPosition = scrollPosition)
}
}
flexible(flex = 1f)
{
VerticalScroller (onScrollPositionChanged = { px: Px, px1: Px ->
scrollPosition.posX = px
scrollPosition.maxX = px1
}){
Column {
for(i in 0..20) {
HeightSpacer(16.dp)
imageBank.forEach { imageItem: ImageItem ->
Text(text = imageItem.title ?: "<Empty>")
Divider()
}
}
}
}
}
}
}
--- compose源码中VerticalScroller Widget的定义:
@Composable
fun VerticalScroller(
scrollerPosition: ScrollerPosition = +memo { ScrollerPosition() },
onScrollPositionChanged: (position: Px, maxPosition: Px) -> Unit = { position, _ ->
scrollerPosition.value = position
},
isScrollable: Boolean = true,
@Children child: @Composable() () -> Unit
) {
Scroller(scrollerPosition, onScrollPositionChanged, true, isScrollable, child)
}