admin管理员组

文章数量:1123703

If I make a column vertically scrollable with Modifier.verticalScroll() then try to detect a drag stop using detectVerticalDragGestures this breaks the scrolling.

Column(
    modifier = Modifier
        .verticalScroll(innerScrollState)
        .pointerInput(Unit) {
            detectVerticalDragGestures(  // <- breaks scrolling
                onDragEnd = {
                    // Do something here
                }
            ) { change, dragAmount ->  }
        }
        .nestedScroll(scrollConnection)
        .fillMaxWidth()
)

What options do I have to detect the user has lifted his finger while maintaining scrollability, including the nestedScroll I have setup.

If I make a column vertically scrollable with Modifier.verticalScroll() then try to detect a drag stop using detectVerticalDragGestures this breaks the scrolling.

Column(
    modifier = Modifier
        .verticalScroll(innerScrollState)
        .pointerInput(Unit) {
            detectVerticalDragGestures(  // <- breaks scrolling
                onDragEnd = {
                    // Do something here
                }
            ) { change, dragAmount ->  }
        }
        .nestedScroll(scrollConnection)
        .fillMaxWidth()
)

What options do I have to detect the user has lifted his finger while maintaining scrollability, including the nestedScroll I have setup.

Share Improve this question asked yesterday user29133385user29133385 11 bronze badge New contributor user29133385 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

2 Answers 2

Reset to default 0

You can check the isScrollInProgress parameter of the scroll state, but I'm not sure it's enough with a custom nestedScroll:

val scrollState = rememberScrollState()
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()

LaunchedEffect(scrollState) {
    snapshotFlow { scrollState.isScrollInProgress }
        .collect { isScrollInProgress ->
            if (!isScrollInProgress) {
                /** doSomething **/
            }
        }
}

Scaffold(
    modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
    topBar = {
        TopAppBar(
            title = { Text("TopAppBar") },
            navigationIcon = {
                IconButton(onClick = { /** doSomething **/ }) {
                    Icon(
                        imageVector = Icons.Filled.Menu,
                        contentDescription = null
                    )
                }
            },
            scrollBehavior = scrollBehavior
        )
    },
) { paddingValues ->
    Column(
        Modifier
            .fillMaxSize()
            .verticalScroll(scrollState)
            .padding(paddingValues)
    ) {
        repeat(200) {
            Text(modifier = Modifier.fillMaxWidth(), text = "Title")
        }
    }
}

In case this helps someone, I dont know if this is the best answer. But if you use pointerInput but don't use detectDragXXX instead do your own, you can observe the values without breaking scrollable. Its not quite as good as dragStart and Stop, but it worked for my needs:

.pointerInput(Unit) {
  awaitPointerEventScope {
    while (true) {
      val event = awaitPointerEvent()
      event.changes.forEach { change ->
        if (change.pressed && !change.previousPressed) { finger down }
        if (!change.pressed) { finger up }
      }
    }
  }
}

本文标签: