Skip to content

Commit 001c37b

Browse files
Fix page navigation callback issues
- Prevent intermediate page callbacks during page snap animations - Ensure onPageChanged fires after scroll/animation completion - Add page snapping to jumpTo() for proper centering on initial load - Fix compilation error with pdfAnimator.isRunning reference - Enhance logging for debugging initial load issues
1 parent a34f4a4 commit 001c37b

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

pdfpreview/src/main/java/com/harissk/pdfpreview/PDFView.kt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,20 @@ class PDFView(context: Context?, attrs: AttributeSet?) : RelativeLayout(context,
415415
when {
416416
isSwipeVertical -> when {
417417
withAnimation -> pdfAnimator.animateVertical(currentYOffset, offset)
418-
else -> moveTo(currentXOffset, offset)
418+
else -> {
419+
moveTo(currentXOffset, offset)
420+
// Perform page snap after jumpTo completes to center the page
421+
if (isPageSnap) performPageSnap()
422+
}
419423
}
420424

421425
else -> when {
422426
withAnimation -> pdfAnimator.animateHorizontal(currentXOffset, offset)
423-
else -> moveTo(offset, currentYOffset)
427+
else -> {
428+
moveTo(offset, currentYOffset)
429+
// Perform page snap after jumpTo completes to center the page
430+
if (isPageSnap) performPageSnap()
431+
}
424432
}
425433
}
426434
showPage(userPage)
@@ -1002,14 +1010,14 @@ class PDFView(context: Context?, attrs: AttributeSet?) : RelativeLayout(context,
10021010
}
10031011

10041012
postDelayed({
1005-
if (!isRecycled && !isRecycling && _pdfFile != null)
1006-
jumpTo(
1007-
page = when {
1008-
currentPage > 0 -> currentPage
1009-
else -> defaultPage
1010-
},
1011-
withAnimation = false
1012-
)
1013+
if (!isRecycled && !isRecycling && _pdfFile != null) {
1014+
val targetPage = when {
1015+
currentPage > 0 -> currentPage
1016+
else -> defaultPage
1017+
}
1018+
logWriter?.writeLog("Performing initial jump to page $targetPage", "PDFView")
1019+
jumpTo(page = targetPage, withAnimation = false)
1020+
}
10131021
}, initDelay)
10141022
} catch (e: Exception) {
10151023
loadError(e)
@@ -1234,6 +1242,10 @@ class PDFView(context: Context?, attrs: AttributeSet?) : RelativeLayout(context,
12341242
internal fun updateScrollUIElements() {
12351243
if (isRecycled || isRecycling) return
12361244

1245+
// Don't report intermediate page numbers during page snap animations
1246+
// to avoid confusing callbacks with pages "in between" the target pages
1247+
if (isPageSnap && pdfAnimator.isFlinging) return
1248+
12371249
when {
12381250
!documentFitsView() -> {
12391251
val positionOffset = positionOffset

pdfpreview/src/main/java/com/harissk/pdfpreview/PdfAnimator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ internal class PdfAnimator(private val pdfView: PDFView) {
6767
if (pdfView.isRecycled) return
6868

6969
pdfView.updateScrollUIElements()
70+
pdfView.loadPageByOffset() // Check if page changed and fire onPageChanged callback
7071
pdfView.loadPages()
7172
isPageAnimating = false
7273
pdfView.scrollHandle?.hideDelayed()
@@ -159,6 +160,7 @@ internal class PdfAnimator(private val pdfView: PDFView) {
159160
flinging = false
160161
if (!pdfView.isRecycled) {
161162
pdfView.updateScrollUIElements()
163+
pdfView.loadPageByOffset() // Check if page changed and fire onPageChanged callback
162164
pdfView.loadPages()
163165
pdfView.scrollHandle?.hideDelayed()
164166
pdfView.performPageSnap()

0 commit comments

Comments
 (0)