Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pager] Make Pager able to scroll by flinging a nested scroll child #1332

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions pager/src/main/java/com/google/accompanist/pager/Pager.kt
Expand Up @@ -385,6 +385,7 @@ internal fun Pager(
ConsumeFlingNestedScrollConnection(
consumeHorizontal = !isVertical,
consumeVertical = isVertical,
pagerState = state,
)
}

Expand Down Expand Up @@ -449,9 +450,11 @@ internal fun Pager(
}
}

@OptIn(ExperimentalPagerApi::class)
private class ConsumeFlingNestedScrollConnection(
private val consumeHorizontal: Boolean,
private val consumeVertical: Boolean,
private val pagerState: PagerState,
) : NestedScrollConnection {
override fun onPostScroll(
consumed: Offset,
Expand All @@ -465,9 +468,15 @@ private class ConsumeFlingNestedScrollConnection(
}

override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
// We can consume all post fling velocity on the main-axis
// so that it doesn't propagate up to the Pager
return available.consume(consumeHorizontal, consumeVertical)
return if (pagerState.currentPageLayoutInfo?.offset != 0) {
Levi-Moreira marked this conversation as resolved.
Show resolved Hide resolved
// The Pager is already scrolling. This means that a nested scroll child was
// scrolled to end, and the Pager can use this fling
Velocity.Zero
} else {
// A nested scroll child is still scrolling. We can consume all post fling
// velocity on the main-axis so that it doesn't propagate up to the Pager
available.consume(consumeHorizontal, consumeVertical)
}
}
}

Expand Down
Expand Up @@ -92,7 +92,7 @@ class PagerState(

internal var afterContentPadding = 0

private val currentPageLayoutInfo: LazyListItemInfo?
internal val currentPageLayoutInfo: LazyListItemInfo?
get() = lazyListState.layoutInfo.visibleItemsInfo.lastOrNull {
it.index == currentPage
}
Expand Down
Expand Up @@ -89,6 +89,17 @@ class HorizontalPagerScrollingContentTest {
// Assert that we're still on page 0
assertThat(pagerState.currentPage).isEqualTo(0)
assertThat(pagerState.currentPageOffset).isWithin(0.01f).of(0f)

// Perform a scroll in the same direction again
rule.onNodeWithTag(TestTag)
.swipeAcrossCenterWithVelocity(velocityPerSec = 2_000.dp, distancePercentageX = -0.5f)

// Wait for the flings to end
rule.waitForIdle()

// Assert that we're now on page 1
assertThat(pagerState.currentPage).isEqualTo(1)
assertThat(pagerState.currentPageOffset).isWithin(0.01f).of(0f)
}

companion object {
Expand Down
Expand Up @@ -88,6 +88,17 @@ class VerticalPagerScrollingContentTest {
// Assert that we're still on page 0
assertThat(pagerState.currentPage).isEqualTo(0)
assertThat(pagerState.currentPageOffset).isWithin(0.01f).of(0f)

// Perform a scroll in the same direction again
rule.onNodeWithTag(TestTag)
.swipeAcrossCenterWithVelocity(velocityPerSec = 2_000.dp, distancePercentageY = -0.5f)

// Wait for the flings to end
rule.waitForIdle()

// Assert that we're now on page 1
assertThat(pagerState.currentPage).isEqualTo(1)
assertThat(pagerState.currentPageOffset).isWithin(0.01f).of(0f)
}

companion object {
Expand Down