Bernardino Alves
Bernardino Alves

Reputation: 43

LazyColumn Swipe to dismiss is triggered when scrolling

I'm using Material3, a standard implementation of a swipe to dismiss:

val dismissState = rememberDismissState(
      confirmValueChange = {
          if ((it == DismissValue.DismissedToStart || it == DismissValue.DismissedToEnd) &&
                                    !listState.isScrollInProgress) {
             viewModel.removeItem(itemActual.idEnvio)
                                    true
          } else false
      },
      positionalThreshold = {
            300.dp.toPx()
      }
)

SwipeToDismiss(
    state = dismissState,
    background = {...},
    dismissContent = {...},
)

So when scrolling up and down, specially when doing it fast a swipe to dismiss is triggered.

Also I can can trigger a swipe to dismiss with a fast touch towards right, barely moving the finger, in the corner of the item of the lazyColumn.

How can I stop this behaviour? This way viewModel.removeItem is triggered inadvertently.

positionalThreshold isn't helping.

Upvotes: 3

Views: 1385

Answers (2)

C. Alen
C. Alen

Reputation: 214

What helped me is to copy the SwipeToDismiss method and set velocityThreshold to 1500.dp. Not sure if there is a better solution, but this worked for me.

Box(
    Modifier.swipeable(
        state = state,
        anchors = anchors,
        thresholds = thresholds,
        orientation = Orientation.Horizontal,
        enabled = state.currentValue == DismissValue.Default,
        reverseDirection = isRtl,
        resistance = ResistanceConfig(
            basis = width,
            factorAtMin = minFactor,
            factorAtMax = maxFactor
        ),
        velocityThreshold = 1500.dp
    )
)

I just added this line

velocityThreshold = 1500.dp

Upvotes: 0

susosaurus
susosaurus

Reputation: 535

How about adding an additional check if scrolling of the LazyColumn is currently in progress?

You could add the following lines to the place where your LazyColumn is implemented:

val listState = rememberLazyListState()
val isScrolling = listState.isScrollInProgress

LazyColumn(state = listState){
   ...
}

and then pass down isScrolling to your swipeable Composable and only allow swipe to dismiss if !isScrolling.

Hope that helps :)

Upvotes: 0

Related Questions