2

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
dbuzin
  • 195
  • 2
  • 13
  • Since Accompanist **0.24.1-alpha**, which requires Compose version **1.2.0-alpha02** or newer, `userScrollEnabled` argument was added to `HorizontalPager`. I don't think there's an other way other than `NestedScrollConnection` if you don't wanna update. – Phil Dukhov Apr 09 '22 at 02:47
  • @PylypDukhov ```userScrollEnabled``` uses the same method under the hood, it's just wrapper above ```NestedScrollConnection``` For now i just remove parent ```HorizontalPager``` with tabs and ```when(tab[index]) { -> SpecificComposable }``` – dbuzin Apr 09 '22 at 09:40
  • Not sure where have you found such source code, in Accompanist **0.24.6-alpha** this parameter is [passed](https://github.com/google/accompanist/blob/80fbb7d6711fdfb4135b175ea03be35550a92d12/pager/src/main/java/com/google/accompanist/pager/Pager.kt#L397) down to `LazyColumn` and it on its turn [pass](https://github.com/androidx/androidx/blob/e8016b3efe56149913c9b0dd8eb720959f42697a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/LazyList.kt#L136) it to scrollable modifier. – Phil Dukhov Apr 09 '22 at 09:57
  • 1
    I've tried disabling first `HorizontalPager` scrolling with `userScrollEnabled` and the second one scrolls fine – Phil Dukhov Apr 09 '22 at 09:57

2 Answers2

2

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

Tippu Fisal Sheriff
  • 2,177
  • 11
  • 19
0

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

Roman
  • 1