Skip to content

Commit

Permalink
fix(android): onPageScroll event gives wrong value
Browse files Browse the repository at this point in the history
  • Loading branch information
iPel authored and zoomchan-cxj committed Nov 9, 2022
1 parent 7fcff68 commit c6656af
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,27 @@ public ViewPagerPageChangeListener(HippyViewPager pager) {

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
mPageScrollEmitter.send(position, positionOffset);
// compute totalOffset from current position/offset to mLastPageIndex
float totalOffset = position - mLastPageIndex + positionOffset;
int toPosition;
if (totalOffset > 0) {
// scrolling forward, convert totalOffset into target position, and offset from (0, 1]
int pageDelta = (int) Math.ceil(totalOffset);
toPosition = mLastPageIndex + pageDelta;
if (pageDelta > 1) {
totalOffset -= pageDelta - 1;
}
} else if (totalOffset < 0) {
// scrolling backward, convert totalOffset into target position, and offset from [-1, 0)
int pageDelta = (int) Math.floor(totalOffset);
toPosition = mLastPageIndex + pageDelta;
if (pageDelta < -1) {
totalOffset -= pageDelta + 1;
}
} else { // not scrolled
toPosition = mLastPageIndex;
}
mPageScrollEmitter.send(toPosition, totalOffset);
}

@Override
Expand Down

0 comments on commit c6656af

Please sign in to comment.