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 cafa7bff0e93bd..27a319e2efbc19 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 @@ -30,6 +30,7 @@ import com.facebook.react.views.view.ReactViewBackgroundManager; import java.lang.reflect.Field; import javax.annotation.Nullable; +import java.util.HashMap; /** * A simple subclass of ScrollView that doesn't dispatch measure and layout to its children and has @@ -59,6 +60,7 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou private @Nullable Drawable mEndBackground; private int mEndFillColor = Color.TRANSPARENT; private View mContentView; + private HashMap mContentOffset = null; private ReactViewBackgroundManager mReactBackgroundManager; public ReactScrollView(ReactContext context) { @@ -131,10 +133,21 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { MeasureSpec.getSize(heightMeasureSpec)); } + public void setContentOffset(HashMap contentOffset) { + mContentOffset = contentOffset; + } + + @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // Call with the present values in order to re-layout if necessary - scrollTo(getScrollX(), getScrollY()); + // If contentOffset is set scroll to its position + if (mContentOffset != null) { + scrollTo(mContentOffset.get("x"), mContentOffset.get("y")); + } else { + // Call with the present values in order to re-layout if necessary + scrollTo(getScrollX(), getScrollY()); + } } @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java index a7d00cd52da60a..3a4d306b43a20f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java @@ -11,6 +11,7 @@ import android.graphics.Color; import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.ReadableMap; import com.facebook.react.common.MapBuilder; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.uimanager.PixelUtil; @@ -24,6 +25,7 @@ import com.facebook.yoga.YogaConstants; import java.util.Map; import javax.annotation.Nullable; +import java.util.HashMap; /** * View manager for {@link ReactScrollView} components. @@ -172,6 +174,25 @@ public void setBorderStyle(ReactScrollView view, @Nullable String borderStyle) { view.setBorderStyle(borderStyle); } + /** + * When set, the scrollview will scroll to the given position on initial layout + * @param view + * @param contentOffset + */ + @ReactProp(name = "contentOffset") + public void setContentOffset(ReactScrollView view, ReadableMap contentOffset) { + if (!contentOffset.hasKey("x") || !contentOffset.hasKey("y")) { + return; + } + + int destX = Math.round(PixelUtil.toPixelFromDIP(contentOffset.getDouble("x"))); + int destY = Math.round(PixelUtil.toPixelFromDIP(contentOffset.getDouble("y"))); + HashMap initialOffset = new HashMap(); + initialOffset.put("x", destX); + initialOffset.put("y", destY); + view.setContentOffset(initialOffset); + } + @ReactPropGroup(names = { ViewProps.BORDER_WIDTH, ViewProps.BORDER_LEFT_WIDTH,