Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vertical ScrollView content offset on Android #15511

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, Integer> mContentOffset = null;
private ReactViewBackgroundManager mReactBackgroundManager;

public ReactScrollView(ReactContext context) {
Expand Down Expand Up @@ -131,10 +133,21 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
MeasureSpec.getSize(heightMeasureSpec));
}

public void setContentOffset(HashMap<String, Integer> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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<String, Integer> initialOffset = new HashMap<String, Integer>();
initialOffset.put("x", destX);
initialOffset.put("y", destY);
view.setContentOffset(initialOffset);
}

@ReactPropGroup(names = {
ViewProps.BORDER_WIDTH,
ViewProps.BORDER_LEFT_WIDTH,
Expand Down