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

Fix maintainVisibleContentPosition on Android during momentum scroll #43425

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void updateScrollPositionInternal() {
int deltaX = newFrame.left - mPrevFirstVisibleFrame.left;
if (deltaX != 0) {
int scrollX = mScrollView.getScrollX();
mScrollView.scrollTo(scrollX + deltaX, mScrollView.getScrollY());
mScrollView.scrollToPreservingMomentum(scrollX + deltaX, mScrollView.getScrollY());
mPrevFirstVisibleFrame = newFrame;
if (mConfig.autoScrollToTopThreshold != null
&& scrollX <= mConfig.autoScrollToTopThreshold) {
Expand All @@ -129,7 +129,7 @@ private void updateScrollPositionInternal() {
int deltaY = newFrame.top - mPrevFirstVisibleFrame.top;
if (deltaY != 0) {
int scrollY = mScrollView.getScrollY();
mScrollView.scrollTo(mScrollView.getScrollX(), scrollY + deltaY);
mScrollView.scrollToPreservingMomentum(mScrollView.getScrollX(), scrollY + deltaY);
mPrevFirstVisibleFrame = newFrame;
if (mConfig.autoScrollToTopThreshold != null
&& scrollY <= mConfig.autoScrollToTopThreshold) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,25 @@ public void scrollTo(int x, int y) {
setPendingContentOffsets(x, y);
}

/**
* Scrolls to a new position preserving any momentum scrolling animation.
*/
public void scrollToPreservingMomentum(int x, int y) {
janicduplessis marked this conversation as resolved.
Show resolved Hide resolved
float velocity = 0;
float direction = 0;
if (mScroller != null && !mScroller.isFinished()) {
velocity = mScroller.getCurrVelocity();
direction = Math.signum(mOnScrollDispatchHelper.getXFlingVelocity());
mScroller.abortAnimation();
}

scrollTo(x, y);

if (mScroller != null && velocity != 0) {
fling((int)(direction * velocity));
}
}

private boolean isContentReady() {
View child = getContentView();
return child != null && child.getWidth() != 0 && child.getHeight() != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,25 @@ public void scrollTo(int x, int y) {
setPendingContentOffsets(x, y);
}

/**
* Scrolls to a new position preserving any momentum scrolling animation.
*/
public void scrollToPreservingMomentum(int x, int y) {
float velocity = 0;
float direction = 0;
if (mScroller != null && !mScroller.isFinished()) {
velocity = mScroller.getCurrVelocity();
direction = Math.signum(mOnScrollDispatchHelper.getYFlingVelocity());
mScroller.abortAnimation();
}

scrollTo(x, y);

if (mScroller != null && velocity != 0) {
fling((int)(direction * velocity));
}
}

private boolean isContentReady() {
View child = getContentView();
return child != null && child.getWidth() != 0 && child.getHeight() != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,5 +597,7 @@ public interface HasScrollEventThrottle {

public interface HasSmoothScroll {
void reactSmoothScrollTo(int x, int y);

void scrollToPreservingMomentum(int x, int y);
}
}
Loading