Skip to content

Commit 87727cf

Browse files
add small offset to left to scroll page
1 parent 8870dd2 commit 87727cf

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

Tutorial1-1Basics/src/main/java/com/smarttoolfactory/tutorial1_1basics/chapter5_gesture/Tutorial5_12SwipePagerToStart.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,24 @@ private fun PagerScrollSample() {
9999
modifier = Modifier
100100
.pointerInput(Unit) {
101101
awaitEachGesture {
102-
awaitFirstDown(pass = PointerEventPass.Initial)
102+
val down = awaitFirstDown(pass = PointerEventPass.Initial)
103103
shouldScrollToFirstPage = false
104104

105+
val firstTouchX = down.position.x
106+
105107
do {
106108
val event: PointerEvent = awaitPointerEvent(
107109
pass = PointerEventPass.Initial
108110
)
109111

110112
event.changes.forEach {
111113

112-
if (pagerState.currentPage == 4 &&
113-
pagerState.currentPage == pagerState.settledPage &&
114-
shouldScrollToFirstPage.not()
115-
) {
114+
val isValid = pagerState.currentPage == 4 &&
115+
pagerState.currentPage == pagerState.settledPage &&
116+
shouldScrollToFirstPage.not()
117+
val movedLeft = it.position.x - firstTouchX < -40f
118+
119+
if (isValid && movedLeft) {
116120
shouldScrollToFirstPage = true
117121
}
118122
}
@@ -190,24 +194,21 @@ private fun PagerScrollSample2() {
190194

191195
event.changes.forEach {
192196

193-
val diff = firstTouchX - it.position.x
197+
val diff = it.position.x - firstTouchX
194198
val posX = it.position.x
195199

196200
val valid = pagerState.currentPage == 4 &&
197201
pagerState.currentPage == pagerState.settledPage &&
198-
// Scroll if user scrolled 10% from first touch position
199-
// or pointer is at the left of 20% of page
200-
(diff > size.width * .10f ||
201-
it.position.x < size.width * .2f) &&
202202
shouldScrollToFirstPage.not()
203203

204204
println(
205205
"Diff $diff, posX: $posX , " +
206206
"current page: ${pagerState.currentPage}, " +
207+
"currentPageOffsetFraction: ${pagerState.currentPageOffsetFraction}" +
207208
"valid: $valid"
208209
)
209210

210-
if (valid) {
211+
if (valid && diff < -40f) {
211212
coroutineScope.launch {
212213
println("🔥 Scrolling...")
213214
shouldScrollToFirstPage = true

0 commit comments

Comments
 (0)