如何从onScrollPositionChanged回调中更改VerticalScroller的scrollerPosition?

时间:2019-11-21 14:27:34

标签: kotlin android-jetpack-compose

如下面的代码所示,我有一个回调,我想在其中收集VerticalScroller的位置,但是如果这样做,VerticalScrollers小部件将不再滚动,因为在源代码中为{{1 }},初始回调会调用VerticalScroller的{​​{1}}。从我要使用它的回调中无法访问此值。有没有一种快速的方法来调用VerticalScroller或使滚动行为继续? (是否创建递归的“竞赛”条件?)

scrollerPosition

---撰写源代码中VerticalScroller窗口小部件的定义:

scrollerPosition

1 个答案:

答案 0 :(得分:0)

实际上,我找到了解决问题的方法...(有时我被教导要开箱即用地思考...)所以...

实际上,在我的@Compose函数中,我需要创建自己的scrollerPosition实例,并在对VerticalScroller的调用中使用它来改变位置。我担心这可能会导致递归的“种族”调用情况,但这取决于在onScrolledPostionChanged的逻辑中何时调用Scroller。如果在onScrolledPositionChanged更改值时调用scrollerPosition.position,则可能会得到递归“竞赛”条件。但显然不是。

所以我的更改基本上是这样的:

@Composable
fun StationsScreen(deviceLocation: LocationData, openDrawer: () -> Unit)
{

    var scrollPosition = ScrollPosition(0.px,0.px)

    var myScrollerPosition = +memo{ ScrollerPosition ()}  //<--- make my own scrollerPosition

    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 (
                scrollerPosition = myScrollerPosition,
                onScrollPositionChanged = { px: Px, px1: Px ->
                    scrollPosition.posX = px
                    scrollPosition.maxX = px1
                    myScrollerPosition.value = px //<-- remember to set the new value here

                    // You can now use the difference of maxX and posX to load
                    // new items into the list...

            }){

                Column {

                    for(i in 0..20) {
                        HeightSpacer(16.dp)
                        imageBank.forEach { imageItem: ImageItem ->

                            Text(text = imageItem.title ?: "<Empty>")

                            Divider()
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun LocationWidget(locationData: LocationData){

    Surface {
        Padding(padding = 8.dp) {
            Text(text = "${locationData.lat}, ${locationData.lng}")
        }
    }

}

@Composable
fun PushWidget(action: () -> Unit){

    Surface {
        Padding(padding = 8.dp) {
            Button(text = "Click Me!", onClick = action)
        }
    }
}

@Composable
fun ScrollPosWidget(scrollPosition: ScrollPosition){
    Surface {
        Padding(padding = 8.dp) {
            Text(text = "posX=${scrollPosition.posX}, maxX=${scrollPosition.maxX}")
        }
    }
}

RG