https://kotlinlang.org logo
#compose
Title
# compose
m

Matija Sokol

03/20/2024, 9:05 AM
Is it possible to decrease swipe sensitivity for
Pager
? In my case,
Pager
item contains
LazyColumn
so there is horizontal Pager swipe and vertical LazyColumn scroll conflict. This is common problem which is easy to fix in View system by overriding
touch slop
property in
ViewPager
. Is same possible in Compose?
a

Albert Chang

03/20/2024, 11:56 AM
You can provide your own
LocalViewConfiguration
with
touchSlop
overridden.
m

Matija Sokol

03/20/2024, 11:58 AM
I tried something like this but still not working as expected (there is still conflict between horizontal/vertical scroll):
Copy code
val viewConfiguration = LocalViewConfiguration.current
val updatedViewConfiguration = remember(viewConfiguration) {
  object : ViewConfiguration by viewConfiguration {
    override val touchSlop: Float
      get() = viewConfiguration.touchSlop * 3
  }
}

Box(modifier = Modifier.fillMaxSize()) {
  CompositionLocalProvider(
    LocalViewConfiguration provides updatedViewConfiguration,
  ) {
    Pager(...)
    }
  }
And then override it again inside page where
LazyColumn
is defined:
Copy code
val viewConfiguration = LocalViewConfiguration.current
val updatedViewConfiguration = remember(viewConfiguration) {
    object : ViewConfiguration by viewConfiguration {
      override val touchSlop: Float
        get() = 5f // decrease for higher sensitivity
    }
  }

HorizontalPager(
    state = pagerState,
    key = ...,
) { index ->
    CompositionLocalProvider(
      LocalViewConfiguration provides updatedViewConfiguration,
    ) {
         LazyColumn(...)
    }
  }
a

Albert Chang

03/20/2024, 12:48 PM
1. You should use the original value inside the pager instead of a fixed value. 2. What do you mean by “conflict”? If you still feel the sensitivity is too high, just increase the value for the pager. This works in the same way as overriding touch slop of ViewPager.
m

Matija Sokol

03/21/2024, 8:55 AM
By "conflict" I mean that often vertical scroll triggers
Pager
and not
LazyColumn
scroll. That is a reason why I want to decrease
Pager
sensitivity. But with this approach (using
CompositionLocalProvider
), when I decrease
Pager
sensitivity, I also change sensitivity for
LazyColumn
since
LazyColumn
is a child of a
Pager
item. Do you have any suggestion to fix that?
a

Albert Chang

03/21/2024, 8:57 AM
As I suggested you should set the touch slop to the original value inside the pager.
u

लातों वाला भूत

03/23/2024, 1:05 AM
@Matija Sokol Were you able to get a solution for this?
8 Views