From 3ed076b65a79cf8438d99d2e031013d8859eec33 Mon Sep 17 00:00:00 2001 From: philvasseur Date: Wed, 4 Apr 2018 20:33:11 -0700 Subject: [PATCH] added in snapAlignment for horizontal android scrollView Summary: `snapToAlignment` is available on iOS but not android yet. This PR is to add support for `snapToAlignment` on android as `snapToInterval` was recently added to android and they are very useful together. Make a `Flatlist` in android with `pagingEnabled`, `horizontal`, `snapToInterval` and `snapToAlignment` set and see how adjusting between the three values of `snapToAlignment` aligns just like it does in iOS. [ANDROID] [MINOR] [ScrollView] - On Android, **ScrollView** now takes snapToAlignment like iOS Closes https://github.com/facebook/react-native/pull/18648 Differential Revision: D7473762 Pulled By: mdvacca fbshipit-source-id: ad4778b83f9fd1352455b2ed28a5f37229d9d8c7 --- .../scroll/ReactHorizontalScrollView.java | 21 ++++++++++++++++++- .../ReactHorizontalScrollViewManager.java | 5 +++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java index 90b9d1fc4..fe4762ac4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java @@ -52,6 +52,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements private @Nullable Drawable mEndBackground; private int mEndFillColor = Color.TRANSPARENT; private int mSnapInterval = 0; + private String mSnapAlignment = "start"; private ReactViewBackgroundManager mReactBackgroundManager; public ReactHorizontalScrollView(Context context) { @@ -96,6 +97,13 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements public void setSnapInterval(int snapInterval) { mSnapInterval = snapInterval; + if(snapInterval != 0) { + mPagingEnabled = true; + } + } + + public void setSnapAlignment(String snapAlignment) { + mSnapAlignment = snapAlignment; } public void flashScrollIndicators() { @@ -236,6 +244,17 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements return getWidth(); } + private int getAlignmentOffset() { + int width = getWidth(); + int snapInterval = getSnapInterval(); + if (mSnapAlignment.equals("center")) { + return (width - snapInterval)/2; + } else if(mSnapAlignment.equals("end")) { + return (width - snapInterval); + } + return 0; + } + public void setEndFillColor(int color) { if (color != mEndFillColor) { mEndFillColor = color; @@ -349,7 +368,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements if (predictedX > page * width + width / 2) { page = page + 1; } - smoothScrollTo(page * width, getScrollY()); + smoothScrollTo(page * width - getAlignmentOffset(), getScrollY()); } @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java index e6536a6e5..d5fe728c3 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java @@ -80,6 +80,11 @@ public class ReactHorizontalScrollViewManager view.setSnapInterval((int) (snapToInterval * screenDisplayMetrics.density)); } + @ReactProp(name = "snapToAlignment") + public void setSnapToAlignment(ReactHorizontalScrollView view, String snapToAlignment) { + view.setSnapAlignment(snapToAlignment); + } + @ReactProp(name = ReactClippingViewGroupHelper.PROP_REMOVE_CLIPPED_SUBVIEWS) public void setRemoveClippedSubviews(ReactHorizontalScrollView view, boolean removeClippedSubviews) { view.setRemoveClippedSubviews(removeClippedSubviews);