Skip to content

Commit 528a3c7

Browse files
Adam ComellaFacebook Github Bot
Adam Comella
authored and
Facebook Github Bot
committed
Android: Keep ScrollView content visible after edits
Summary: Suppose that the user is scrolled to the bottom of a ScrollView. Next, the ScrollView's content is edited such that the height of the content changes and the current scroll position is larger than the new height of the content. Consequently, the user sees a blank ScrollView. As soon as the user interacts with the ScrollView, the ScrollView will jump to its max scroll position. This change improves this scenario by ensuring that the user is never staring at a blank ScrollView when the ScrollView has content in it. It does this by moving the ScrollView to its max scroll position when the scroll position after an edit is larger than the max scroll position of the ScrollView. Here are some pictures to illustrate how this PR improves the scenario described above: ![image](https://cloud.githubusercontent.com/assets/199935/20408839/0e731774-accc-11e6-9f0a-3d77198645e9.png) ![image](https://cloud.githubusercontent.com/assets/199935/20408844/12877bb6-accc-11e6-8fe2-1c1bb26569cc.png) **Test plan (require Closes #11000 Differential Revision: D4250792 Pulled By: astreet fbshipit-source-id: 940fff6282ad29c796726f68b4519cbdabbfe554
1 parent eda09f8 commit 528a3c7

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java

+43-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.util.Log;
2222
import android.view.MotionEvent;
2323
import android.view.View;
24+
import android.view.ViewGroup;
2425
import android.widget.OverScroller;
2526
import android.widget.ScrollView;
2627

@@ -39,7 +40,7 @@
3940
* <p>ReactScrollView only supports vertical scrolling. For horizontal scrolling,
4041
* use {@link ReactHorizontalScrollView}.
4142
*/
42-
public class ReactScrollView extends ScrollView implements ReactClippingViewGroup {
43+
public class ReactScrollView extends ScrollView implements ReactClippingViewGroup, ViewGroup.OnHierarchyChangeListener, View.OnLayoutChangeListener {
4344

4445
private static Field sScrollerField;
4546
private static boolean sTriedToGetScrollerField = false;
@@ -58,6 +59,7 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou
5859
private @Nullable String mScrollPerfTag;
5960
private @Nullable Drawable mEndBackground;
6061
private int mEndFillColor = Color.TRANSPARENT;
62+
private View mContentView;
6163

6264
public ReactScrollView(ReactContext context) {
6365
this(context, null);
@@ -98,6 +100,8 @@ public ReactScrollView(ReactContext context, @Nullable FpsListener fpsListener)
98100
} else {
99101
mScroller = null;
100102
}
103+
104+
setOnHierarchyChangeListener(this);
101105
}
102106

103107
public void setSendMomentumEvents(boolean sendMomentumEvents) {
@@ -299,6 +303,12 @@ private boolean isScrollPerfLoggingEnabled() {
299303
return mFpsListener != null && mScrollPerfTag != null && !mScrollPerfTag.isEmpty();
300304
}
301305

306+
private int getMaxScrollY() {
307+
int contentHeight = mContentView.getHeight();
308+
int viewportHeight = getHeight() - getPaddingBottom() - getPaddingTop();
309+
return Math.max(0, contentHeight - viewportHeight);
310+
}
311+
302312
@Override
303313
public void draw(Canvas canvas) {
304314
if (mEndFillColor != Color.TRANSPARENT) {
@@ -327,9 +337,7 @@ protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolea
327337
// more information.
328338

329339
if (!mScroller.isFinished() && mScroller.getCurrY() != mScroller.getFinalY()) {
330-
int scrollRange = Math.max(
331-
0,
332-
getChildAt(0).getHeight() - (getHeight() - getPaddingBottom() - getPaddingTop()));
340+
int scrollRange = getMaxScrollY();
333341
if (scrollY >= scrollRange) {
334342
mScroller.abortAnimation();
335343
scrollY = scrollRange;
@@ -341,4 +349,35 @@ protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolea
341349

342350
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
343351
}
352+
353+
@Override
354+
public void onChildViewAdded(View parent, View child) {
355+
mContentView = child;
356+
mContentView.addOnLayoutChangeListener(this);
357+
}
358+
359+
@Override
360+
public void onChildViewRemoved(View parent, View child) {
361+
mContentView.removeOnLayoutChangeListener(this);
362+
mContentView = null;
363+
}
364+
365+
/**
366+
* Called when a mContentView's layout has changed. Fixes the scroll position if it's too large
367+
* after the content resizes. Without this, the user would see a blank ScrollView when the scroll
368+
* position is larger than the ScrollView's max scroll position after the content shrinks.
369+
*/
370+
@Override
371+
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
372+
if (mContentView == null) {
373+
return;
374+
}
375+
376+
int currentScrollY = getScrollY();
377+
int maxScrollY = getMaxScrollY();
378+
if (currentScrollY > maxScrollY) {
379+
scrollTo(getScrollX(), maxScrollY);
380+
}
381+
}
344382
}
383+

0 commit comments

Comments
 (0)