Skip to content

Commit 2bbea50

Browse files
Nick Lefevermeta-codesync[bot]
authored andcommitted
Restore scroll state of scroll view on mount (#54000)
Summary: Pull Request resolved: #54000 Update the scroll view manager to load the fabric state and restore the scroll position and scroll away top padding on mount. Restoring the scroll view happens in the react scroll view state setter, which gets called by the `ReactScrollViewHelper` with the deserialized state provided on mount. The state loading will happen only until the scroll view itself submits a new state to Fabric (due to scrolling). This guarantees that we only restore the initial state on mount. This diff also updates the scroll away top padding setter to support setting a new value without triggering a fabric state update. Changelog: [Internal] Reviewed By: rshest Differential Revision: D83247016 fbshipit-source-id: 7ae44b9f69c58da9e5163574073767aea2713b62
1 parent aa85da3 commit 2bbea50

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5779,6 +5779,7 @@ public class com/facebook/react/views/scroll/ReactScrollView : android/widget/Sc
57795779
public fun setReactScrollViewScrollState (Lcom/facebook/react/views/scroll/ReactScrollViewHelper$ReactScrollViewScrollState;)V
57805780
public fun setRemoveClippedSubviews (Z)V
57815781
public fun setScrollAwayTopPaddingEnabledUnstable (I)V
5782+
public fun setScrollAwayTopPaddingEnabledUnstable (IZ)V
57825783
public fun setScrollEnabled (Z)V
57835784
public fun setScrollEventThrottle (I)V
57845785
public fun setScrollPerfTag (Ljava/lang/String;)V

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.content.Context;
1919
import android.graphics.Canvas;
2020
import android.graphics.Color;
21+
import android.graphics.Point;
2122
import android.graphics.Rect;
2223
import android.graphics.drawable.ColorDrawable;
2324
import android.graphics.drawable.Drawable;
@@ -1439,6 +1440,10 @@ public void setBorderStyle(@Nullable String style) {
14391440
* style. `translateY` must never be set from ReactJS while using this feature!
14401441
*/
14411442
public void setScrollAwayTopPaddingEnabledUnstable(int topPadding) {
1443+
setScrollAwayTopPaddingEnabledUnstable(topPadding, true);
1444+
}
1445+
1446+
public void setScrollAwayTopPaddingEnabledUnstable(int topPadding, boolean updateState) {
14421447
int count = getChildCount();
14431448

14441449
Assertions.assertCondition(
@@ -1458,7 +1463,9 @@ public void setScrollAwayTopPaddingEnabledUnstable(int topPadding) {
14581463
setPadding(0, 0, 0, topPadding);
14591464
}
14601465

1461-
updateScrollAwayState(topPadding);
1466+
if (updateState) {
1467+
updateScrollAwayState(topPadding);
1468+
}
14621469
setRemoveClippedSubviews(mRemoveClippedSubviews);
14631470
}
14641471

@@ -1470,6 +1477,12 @@ private void updateScrollAwayState(int scrollAwayPaddingTop) {
14701477
@Override
14711478
public void setReactScrollViewScrollState(ReactScrollViewScrollState scrollState) {
14721479
mReactScrollViewScrollState = scrollState;
1480+
if (ReactNativeFeatureFlags.enableViewCulling()) {
1481+
setScrollAwayTopPaddingEnabledUnstable(scrollState.getScrollAwayPaddingTop(), false);
1482+
1483+
Point scrollPosition = scrollState.getLastStateUpdateScroll();
1484+
scrollTo(scrollPosition.x, scrollPosition.y);
1485+
}
14731486
}
14741487

14751488
@Override

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ constructor(private val fpsListener: FpsListener? = null) :
384384
stateWrapper: StateWrapper,
385385
): Any? {
386386
view.setStateWrapper(stateWrapper)
387+
if (ReactNativeFeatureFlags.enableViewCulling()) {
388+
ReactScrollViewHelper.loadFabricScrollState(view, stateWrapper)
389+
}
387390
return null
388391
}
389392

0 commit comments

Comments
 (0)