Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
Add "contentOffset" property to ScrollView
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryn authored and yenda committed Nov 29, 2018
1 parent 44bfffb commit 0b6d361
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@

import java.lang.reflect.Field;
import java.util.List;
import java.util.HashMap;
import javax.annotation.Nullable;


/**
* A simple subclass of ScrollView that doesn't dispatch measure and layout to its children and has
* a scroll listener to send scroll events to JS.
Expand Down Expand Up @@ -71,6 +73,7 @@ public class ReactScrollView extends ScrollView implements ReactClippingViewGrou
private @Nullable List<Integer> mSnapOffsets;
private View mContentView;
private ReactViewBackgroundManager mReactBackgroundManager;
private HashMap<String, Integer> mContentOffset = null;

public ReactScrollView(ReactContext context) {
this(context, null);
Expand Down Expand Up @@ -175,10 +178,20 @@ 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 @@ -13,6 +13,7 @@
import android.util.DisplayMetrics;

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.DisplayMetricsHolder;
Expand All @@ -28,6 +29,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -211,6 +213,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

0 comments on commit 0b6d361

Please sign in to comment.