ModalBottomSheetLayout 在 Compose 1.4.0 及更高版本中在 flinging 上跳跃

问题描述 投票:0回答:1

我正在寻找关闭这种“跳跃”的可能性

https://www.veed.io/view/0f7585b4-529f-4700-8dc2-15788164fa44

我在 1.4.0 以下的 Compose 版本上看不到它。 我的代码:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val scope = rememberCoroutineScope()
            val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
            ModalBottomSheetLayout(
                sheetState = sheetState,
                sheetContent = {
                    Text(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(200.dp),
                        text = "CONTENT"
                    )
                }
            ) {
                Button(onClick = { scope.launch { sheetState.show() } }) {
                    Text(text = "MODAL")
                }
            }
        }
    }
}

我正在寻找关闭此“跳跃功能”的可能性。

PS。看起来已经存在问题了https://issuetracker.google.com/issues/285847707,但也许有人有解决方法?

android android-jetpack-compose bottom-sheet
1个回答
0
投票
package com.lalilu.component.extension

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.unit.Velocity
import kotlin.math.abs

class BottomSheetNestedScrollInterceptor : NestedScrollConnection {
    private var arrivedBoundarySource: NestedScrollSource? = null

    override fun onPreScroll(
        available: Offset,
        source: NestedScrollSource
    ): Offset {
        // 重置到达边界时的状态
        if (source == NestedScrollSource.Drag && arrivedBoundarySource == NestedScrollSource.Fling) {
            arrivedBoundarySource = null
        }

        return super.onPreScroll(available, source)
    }

    override fun onPostScroll(
        consumed: Offset,
        available: Offset,
        source: NestedScrollSource
    ): Offset {
        // 子布局无法消费完即到达边界
        if (arrivedBoundarySource == null && abs(available.y) > 0) {
            arrivedBoundarySource = source
        }

        // 根据到达边界时的子布局消费情况决定是否消费
        if (arrivedBoundarySource == NestedScrollSource.Fling) {
            return available
        }

        return Offset.Zero
    }

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

@Composable
fun rememberBottomSheetNestedScrollInterceptor(): BottomSheetNestedScrollInterceptor {
    return remember { BottomSheetNestedScrollInterceptor() }
}

用sheetContent中的

Modifier.nestedScroll
试试这个,不知道Google要花多长时间才能解决这个问题,所以我必须自己解决,但这不是任何EdgeEffect,滚动到边缘时似乎不太好.

© www.soinside.com 2019 - 2024. All rights reserved.