Skip to content

Commit

Permalink
Add snapToInterval support for Android ScrollView
Browse files Browse the repository at this point in the history
  • Loading branch information
sigvef committed Aug 26, 2017
1 parent dc22bd6 commit 25aac4f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 0 additions & 2 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,6 @@ const ScrollView = createReactClass({
* that have lengths smaller than the scroll view. Typically used in
* combination with `snapToAlignment` and `decelerationRate="fast"`.
* Overrides less configurable `pagingEnabled` prop.
*
* @platform ios
*/
snapToInterval: PropTypes.number,
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

import com.facebook.infer.annotation.Assertions;
import com.facebook.react.uimanager.MeasureSpecAssertions;
import com.facebook.react.uimanager.events.NativeGestureUtil;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactClippingViewGroup;
import com.facebook.react.uimanager.ReactClippingViewGroupHelper;
import com.facebook.react.uimanager.events.NativeGestureUtil;
import com.facebook.react.views.view.ReactViewBackgroundDrawable;

/**
Expand All @@ -43,6 +44,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements
private @Nullable Rect mClippingRect;
private boolean mDragging;
private boolean mPagingEnabled = false;
private double mSnapToInterval;
private @Nullable Runnable mPostTouchRunnable;
private boolean mRemoveClippedSubviews;
private boolean mScrollEnabled = true;
Expand Down Expand Up @@ -92,6 +94,10 @@ public void setPagingEnabled(boolean pagingEnabled) {
mPagingEnabled = pagingEnabled;
}

public void setSnapToInterval(double snapToInterval) {
mSnapToInterval = snapToInterval;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
MeasureSpecAssertions.assertExplicitMeasureSpec(widthMeasureSpec, heightMeasureSpec);
Expand Down Expand Up @@ -233,6 +239,13 @@ private void disableFpsListener() {
}
}

private int getPageWidth() {
if(mSnapToInterval != 0) {
return (int) (PixelUtil.toPixelFromDIP(mSnapToInterval) + 0.5);
}
return getWidth();
}

private boolean isScrollPerfLoggingEnabled() {
return mFpsListener != null && mScrollPerfTag != null && !mScrollPerfTag.isEmpty();
}
Expand Down Expand Up @@ -310,7 +323,7 @@ public void run() {
* scrolling.
*/
private void smoothScrollToPage(int velocity) {
int width = getWidth();
int width = getPageWidth();
int currentX = getScrollX();
// TODO (t11123799) - Should we do anything beyond linear accounting of the velocity
int predictedX = currentX + velocity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public void setPagingEnabled(ReactHorizontalScrollView view, boolean pagingEnabl
view.setPagingEnabled(pagingEnabled);
}

@ReactProp(name = "snapToInterval")
public void setSnapToInterval(ReactHorizontalScrollView view, double snapToInterval) {
view.setSnapToInterval(snapToInterval);
}

/**
* Controls overScroll behaviour
*/
Expand Down

0 comments on commit 25aac4f

Please sign in to comment.