我有一行,其中包含一个lazyColumn和一个定制布局.LAZY列包含小时数和包含定位的事件的自定义布局.它们的滚动状态不同,因此不会一起滚动.我怎么才能解决这个问题.因为我需要一个懒惰专栏来跟踪我在使用布局信息的那一天.

@Composable
fun ScheduleSidebar(
    hourHeight: Dp = 64.dp){
    LazyColumn(
        modifier = modifier,
    
    ) {
        val startTime = LocalTime.MIN
        items(168) { hour ->
            Box(modifier = Modifier.height(hourHeight)) {
                Text(startTime.plusHours(hour.toLong()))
            }
        
        }

    }

这是我的观点:

@Composable
fun ScheduleView(
    viewModel: ScheduleViewModel = getViewModel()
) {
    val verticalScrollState = rememberScrollState()
    val ss = rememberLazyListState()
    val scrollConnection = rememberNestedScrollInteropConnection()

     //added this and still the lazycolumn can scroll alone

LaunchedEffect(key1 = Unit) {



   var oldValue = 0
    snapshotFlow {
        verticalScrollState.value
    }.collect {
        println("Value: $it")
        ss.scrollBy((it-oldValue).toFloat())
        oldValue = it
    }
}


    Column(modifier = Modifier.fillMaxSize()) {


        ScheduleHeader()

        Row(
            modifier = Modifier
                .weight(1f)
        ) {

            ScheduleSidebar(
                hourHeight = 64.dp,
                modifier = Modifier
               
                ,
             
            
          
            )

            Schedule(
                bookings = viewModel.state.value.bookingsList,
                modifier = Modifier
                    .verticalScroll(verticalScrollState),
            
           
                onEmptyViewClicked = { days, hour, selectedDate ->
                    showSheet = true
                    viewModel.onEvent(
                        ScheduleEvent.OnEmptyViewClicked(
                            days = days,
                            startTime = hour,
                            selectedDate = selectedDate
                        )
                    )

                }
            )
        }


    }
}

推荐答案

您可以使用

val state = rememberScrollState()
val lazyListState = rememberLazyListState()

LaunchedEffect(key1 = Unit) {

    var oldValue = 0
    snapshotFlow {
        state.value
    }.collect {
        println("Value: $it")
        lazyListState.scrollBy((it -oldValue).toFloat())

        oldValue = it
    }
}

滚动带有垂直滚动的列时滚动LazyColumn.

由于它不可重现或没有图像达到预期效果,因此制作了一个示例来展示如何使用1值滚动

enter image description here

@Preview
@Composable
private fun LayoutTest() {

    val state = rememberScrollState()
    val lazyListState = rememberLazyListState()

    LaunchedEffect(key1 = Unit) {

        var oldValue = 0
        snapshotFlow {
            state.value
        }.collect {
            println("Value: $it")
            lazyListState.scrollBy((it - oldValue).toFloat())

            oldValue = it
        }
    }

    Column(
        Modifier
            .fillMaxSize()
            .border(2.dp, Color.Red)
    ) {


        Row(
            Modifier
                .fillMaxSize()
                .border(3.dp, Color.Green)
        ) {
            Column(
                modifier = Modifier
                    .width(150.dp)
                    .fillMaxHeight()
                    .verticalScroll(state = state)
            ) {
                for (i in 0..99) {
                    Text(
                        text = "Header $i",
                        fontSize = 20.sp,
                        color = Color.White,
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(Color.Green)
                    )
                }
            }

            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth(),
                state = lazyListState
            ) {
                items(100) {
                    Text(
                        text = "Row $it",
                        fontSize = 20.sp,
                        color = Color.White,
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(Color.Red)
                    )
                }
            }
        }
    }
}

编辑

如果您希望同时滚动可滚动列和LazyColumn,可以使用NestedScrollConnection获取LazyColumn的滚动值,并使用scroll stat禁用LaunchedEffect侦听列滚动以不再进一步滚动LazyColumn.

但是,如果您在滚动其他合成组件的过程中开始滚动,这将无法正常工作.

@Preview
@Composable
private fun LayoutTest() {

    val state = rememberScrollState()
    val lazyListState = rememberLazyListState()

    var listScrolling by remember {
        mutableStateOf(false)
    }

    val coroutineScope = rememberCoroutineScope()

    val nestedScrollConnection = remember {
        object : NestedScrollConnection {
            override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
                val delta = -available.y
                coroutineScope.launch {
                    state.scrollBy(delta)
                    listScrolling = true
                }
                return Offset.Zero
            }

            override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
                listScrolling = false
                return super.onPostFling(consumed, available)
            }
        }
    }


    LaunchedEffect(key1 = Unit) {
        var oldValue = 0
        snapshotFlow {
            state.value
        }.collect {
            if (listScrolling.not() && lazyListState.isScrollInProgress.not()) {
                lazyListState.scrollBy((it - oldValue).toFloat())
                oldValue = it
            }
        }
    }

    Column(
        Modifier
            .fillMaxSize()
            .padding(8.dp)
    ) {

        Row(
            Modifier
                .fillMaxSize()
        ) {
            Column(
                modifier = Modifier
                    .width(150.dp)
                    .fillMaxHeight()
                    .verticalScroll(state = state)
            ) {
                for (i in 0..400) {
                    Text(
                        text = "Header $i",
                        fontSize = 24.sp,
                        modifier = Modifier
                            .shadow(4.dp, RoundedCornerShape(8.dp))
                            .fillMaxWidth()
                            .background(Color.White, RoundedCornerShape(8.dp))
                            .padding(8.dp)
                            .wrapContentSize()
                    )
                }
            }


            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .nestedScroll(nestedScrollConnection),
                state = lazyListState
            ) {
                items(401) {
                    Text(
                        text = "Row $it",
                        fontSize = 24.sp,
                        modifier = Modifier
                            .shadow(4.dp, RoundedCornerShape(8.dp))
                            .fillMaxWidth()
                            .background(Color.White, RoundedCornerShape(8.dp))
                            .padding(8.dp)
                            .wrapContentSize()
                    )
                }
            }
        }
    }
}

编辑2

这个看起来在任何情况下都很好用

@Preview
@Composable
private fun LayoutTest2() {

    val state = rememberScrollState()
    val lazyListState = rememberLazyListState()
    
    val coroutineScope = rememberCoroutineScope()

    val nestedScrollConnection = remember {
        object : NestedScrollConnection {
            override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
                val delta = -available.y
                if (state.isScrollInProgress.not()) {
                    coroutineScope.launch {
                        state.scrollBy(delta)
                    }
                }

                if (lazyListState.isScrollInProgress.not()) {
                    coroutineScope.launch {
                        lazyListState.scrollBy(delta)
                    }
                }
                return Offset.Zero
            }

        }
    }

    Column(
        Modifier
            .fillMaxSize()
            .nestedScroll(nestedScrollConnection)
            .padding(8.dp)
    ) {

        Row(
            Modifier
                .fillMaxSize()
        ) {
            Column(
                modifier = Modifier
                    .width(150.dp)
                    .fillMaxHeight()
                    .verticalScroll(state = state)
            ) {
                for (i in 0..400) {
                    Text(
                        text = "Header $i",
                        fontSize = 24.sp,
                        modifier = Modifier
                            .shadow(4.dp, RoundedCornerShape(8.dp))
                            .fillMaxWidth()
                            .background(Color.White, RoundedCornerShape(8.dp))
                            .padding(8.dp)
                            .wrapContentSize()
                    )
                }
            }

            LazyColumn(
                state = lazyListState,
                modifier = Modifier
                    .fillMaxWidth()
            ) {
                items(401) {
                    Text(
                        text = "Row $it",
                        fontSize = 24.sp,
                        modifier = Modifier
                            .shadow(4.dp, RoundedCornerShape(8.dp))
                            .fillMaxWidth()
                            .background(Color.White, RoundedCornerShape(8.dp))
                            .padding(8.dp)
                            .wrapContentSize()
                    )
                }
            }
        }
    }
}

Android相关问答推荐

写入排除例外以进行依赖性判断

如何在Android Emulator上从物理设备接收TCP消息

用于小部件泄漏警告的伙伴对象中的Kotlin Lateinit

如何在使用带有底部导航组件的片段管理器时更改片段工具栏的标签

布局可组合项如何具有可测量和约束参数?

可组合函数无限地从视图模型获取值

使用 Gadle kotlin 为多模块 Android 代码库设置 jacoco

Gradle在我的Android Compose项目中继续推广依赖版本

同样的参数值下,为什么requiredSizeIn不等于requiredWidthIn + requiredHeightIn?

Android WebView 不会在滚动端加载新内容

如何在 Delphi 和 Android 上避免 Indy Socket Error #13 Access denied 异常?

用作输入参数的 Lambda 函数导致重组

来自位图的 WearOS 图标不显示 colored颜色

在 Jetpack Compose 中包装内容

使用默认使用 RTL 语言的项目本地化 android 应用程序

无法为:app@debug/compileClasspath解析依赖项com.github.dhaval2404:imagepicker-support:1.7.1

如何将设备屏幕位置转换为发送事件位置?

android 13 版本是否会影响 android 12 目标应用程序

如何将新的 ComponentActivity 与 ViewBinding 和其他旧的 AppCompatActivity 组件一起使用

CenterAlignedTopAppBar 滚动行为:未为参数状态传递值