Skip to content

Commit

Permalink
last item removal scroll position restore
Browse files Browse the repository at this point in the history
  • Loading branch information
jayshah123 committed Dec 21, 2019
1 parent a8fbbe2 commit 8b363f9
Showing 1 changed file with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.OverScroller;
import androidx.annotation.Nullable;
Expand All @@ -39,7 +40,8 @@

/** Similar to {@link ReactScrollView} but only supports horizontal scrolling. */
public class ReactHorizontalScrollView extends HorizontalScrollView
implements ReactClippingViewGroup {
implements ReactClippingViewGroup, ViewGroup.OnHierarchyChangeListener,
View.OnLayoutChangeListener {

private static @Nullable Field sScrollerField;
private static boolean sTriedToGetScrollerField = false;
Expand Down Expand Up @@ -68,6 +70,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView
private @Nullable List<Integer> mSnapOffsets;
private boolean mSnapToStart = true;
private boolean mSnapToEnd = true;
private View mContentView;
private ReactViewBackgroundManager mReactBackgroundManager;
private boolean mPagedArrowScrolling = false;

Expand All @@ -83,6 +86,7 @@ public ReactHorizontalScrollView(Context context, @Nullable FpsListener fpsListe
mFpsListener = fpsListener;

mScroller = getOverScrollerFromParent();
setOnHierarchyChangeListener(this);
}

@Nullable
Expand Down Expand Up @@ -537,6 +541,51 @@ protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolea
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}

private int getMaxScrollX() {
int contentWidth = mContentView.getWidth();
int viewportWidth = getWidth() - getPaddingLeft() - getPaddingRight();
return Math.max(0, contentWidth - viewportWidth);
}

@Override
public void onChildViewAdded(View parent, View child) {
mContentView = child;
mContentView.addOnLayoutChangeListener(this);
}

@Override
public void onChildViewRemoved(View parent, View child) {
mContentView.removeOnLayoutChangeListener(this);
mContentView = null;
}

/**
* Called when a mContentView's layout has changed. Fixes the scroll position if it's too large
* after the content resizes. Without this, the user would see a blank ScrollView when the scroll
* position is larger than the ScrollView's max scroll position after the content shrinks.
*/
@Override
public void onLayoutChange(
View v,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
if (mContentView == null) {
return;
}

int currentScrollX = getScrollX();
int maxScrollX = getMaxScrollX();
if (currentScrollX > maxScrollX) {
scrollTo(maxScrollX, getScrollY());
}
}

private void enableFpsListener() {
if (isScrollPerfLoggingEnabled()) {
Assertions.assertNotNull(mFpsListener);
Expand Down

0 comments on commit 8b363f9

Please sign in to comment.