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 29, 2017
1 parent b11656a commit 0d4ed33
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 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 @@ -22,6 +22,7 @@
import android.widget.HorizontalScrollView;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.uimanager.MeasureSpecAssertions;
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;
Expand All @@ -41,6 +42,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 @@ -94,6 +96,10 @@ public void flashScrollIndicators() {
awakenScrollBars();
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
MeasureSpecAssertions.assertExplicitMeasureSpec(widthMeasureSpec, heightMeasureSpec);
Expand Down Expand Up @@ -235,6 +241,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 @@ -312,7 +325,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 @@ -105,6 +105,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 0d4ed33

Please sign in to comment.