From 115dbe9433464d78dcc98db3bd0ef423670345b6 Mon Sep 17 00:00:00 2001 From: Genki Kondo Date: Mon, 30 Jan 2023 14:06:37 -0800 Subject: [PATCH] In onLayoutChange, only scroll if the view is shown and the content view is ready Summary: ScrollViews don't properly maintain position where they are hidden and shown. When a ScrollView's content is laid out, onLayoutChange is triggered. This is also fired when the views are hidden, which is not desirable as the layout may not be accurate when the view is hidden. Check that the scroll view is showing before attempting a scroll. Changelog: [Internal][Fixed] - In onLayoutChange, only scroll if the view is shown and the content view is ready Reviewed By: sshic Differential Revision: D42808119 fbshipit-source-id: 0197ae55fa7d80e52c2ea483609e62d512a117f3 --- .../facebook/react/views/scroll/ReactScrollView.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java index e42e648f56a6f7..2442f7b2013d55 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java @@ -1127,10 +1127,12 @@ public void onLayoutChange( mMaintainVisibleContentPositionHelper.updateScrollPosition(); } - int currentScrollY = getScrollY(); - int maxScrollY = getMaxScrollY(); - if (currentScrollY > maxScrollY) { - scrollTo(getScrollX(), maxScrollY); + if (isShown() && isContentReady()) { + int currentScrollY = getScrollY(); + int maxScrollY = getMaxScrollY(); + if (currentScrollY > maxScrollY) { + scrollTo(getScrollX(), maxScrollY); + } } }