dbuzin
dbuzin

Reputation: 365

Accompanist Nested HorizontalPager disable swipes in parent

I have a nested HorizontalPager like

HorizontalPager(
   count = list.size
) {
   HorizontalPager(
    count = list2.size
   ) {
     //items
   }
 }

is there any way to disable horizontal scrolling in parents pager, but enable in childrens.

This solution disables scroll in all childs views and it isn't what i need

private val HorizontalScrollConsumer = object : NestedScrollConnection {
    override fun onPreScroll(available: Offset, source: NestedScrollSource) = available.copy(y = 0f)
    override suspend fun onPreFling(available: Velocity) = available.copy(y = 0f)
}

fun Modifier.disabledHorizontalPointerInputScroll(disabled: Boolean = true) =
    if (disabled) this.nestedScroll(HorizontalScrollConsumer) else this

Upvotes: 3

Views: 2487

Answers (2)

Roman
Roman

Reputation: 21

Try this:

 HorizontalPager(
   count = 2
) { index ->
   when(index) {
       0 -> {
          //...
       }
       1 -> {
          Box(modifier = Modifier.blockOuterScrolling()) {
              HorizontalPager(count = list2.size) {
                //items
              }
          }
       }
   }
}

Where .blockOuterScrolling() is:

fun Modifier.blockOuterScrolling() =
    then(Modifier.nestedScroll(
        object : NestedScrollConnection {
            override fun onPreScroll(
                available: Offset,
                source: NestedScrollSource
            ) = Offset.Zero

            override fun onPostScroll(
                consumed: Offset,
                available: Offset,
                source: NestedScrollSource
            ) = available
        }
    ))

Good luck

Upvotes: 2

Tippu Fisal Sheriff
Tippu Fisal Sheriff

Reputation: 2846

Pass this argument in HorizontalPager

userScrollEnabled = false

HorizontalPager(
    ...
    userScrollEnabled = false
    ...
) {
  // Your Code...
}

Note: By default userScrollEnabled is true, if we need to disable it, just pass this argument with false in HorizontalPager

Upvotes: 2

Related Questions