From 3b68869fc83e9ea02ea0527cb1c32ae8df209738 Mon Sep 17 00:00:00 2001 From: James Ide Date: Wed, 23 Sep 2015 05:57:00 -0700 Subject: [PATCH] Add scrollWithoutAnimationTo to Android MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: `ScrollView.scrollWithoutAnimationTo` is supported on iOS but not Android. This is an existing API, and this diff adds Android support. Closes https://github.com/facebook/react-native/pull/2695 Reviewed By: @​svcscm Differential Revision: D2452630 Pulled By: @mkonicek --- Libraries/Components/ScrollResponder.js | 20 +++++++++++++++++++ Libraries/Components/ScrollView/ScrollView.js | 6 +++--- .../ReactHorizontalScrollViewManager.java | 7 +++++++ .../scroll/ReactScrollViewCommandHelper.java | 17 ++++++++++++++-- .../views/scroll/ReactScrollViewManager.java | 7 +++++++ 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Libraries/Components/ScrollResponder.js b/Libraries/Components/ScrollResponder.js index b0f132075..1b5ea15f1 100644 --- a/Libraries/Components/ScrollResponder.js +++ b/Libraries/Components/ScrollResponder.js @@ -366,6 +366,26 @@ var ScrollResponderMixin = { } }, + /** + * Like `scrollResponderScrollTo` but immediately scrolls to the given + * position + */ + scrollResponderScrollWithouthAnimationTo: function(offsetX: number, offsetY: number) { + if (Platform.OS === 'android') { + RCTUIManager.dispatchViewManagerCommand( + React.findNodeHandle(this), + RCTUIManager.RCTScrollView.Commands.scrollWithoutAnimationTo, + [offsetX, offsetY], + ); + } else { + RCTUIManager.scrollWithoutAnimationTo( + React.findNodeHandle(this), + offsetX, + offsetY + ); + } + }, + /** * A helper function to zoom to a specific rect in the scrollview. * @param {object} rect Should have shape {x, y, width, height} diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index 1ee37a84f..7c1fc9dca 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -290,10 +290,10 @@ var ScrollView = React.createClass({ }, scrollWithoutAnimationTo: function(destY?: number, destX?: number) { - RCTUIManager.scrollWithoutAnimationTo( - React.findNodeHandle(this), + // $FlowFixMe - Don't know how to pass Mixin correctly. Postpone for now + this.getScrollResponder().scrollResponderScrollWithouthAnimationTo( destX || 0, - destY || 0 + destY || 0, ); }, 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 aa8c784cc..698ea9262 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 @@ -51,4 +51,11 @@ public class ReactHorizontalScrollViewManager ReactScrollViewCommandHelper.ScrollToCommandData data) { scrollView.smoothScrollTo(data.mDestX, data.mDestY); } + + @Override + public void scrollWithoutAnimationTo( + ReactHorizontalScrollView scrollView, + ReactScrollViewCommandHelper.ScrollToCommandData data) { + scrollView.scrollTo(data.mDestX, data.mDestY); + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.java index 840fde9dd..586e89506 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewCommandHelper.java @@ -25,9 +25,11 @@ import com.facebook.react.common.MapBuilder; public class ReactScrollViewCommandHelper { public static final int COMMAND_SCROLL_TO = 1; + public static final int COMMAND_SCROLL_WITHOUT_ANIMATION_TO = 2; public interface ScrollCommandHandler { void scrollTo(T scrollView, ScrollToCommandData data); + void scrollWithoutAnimationTo(T scrollView, ScrollToCommandData data); } public static class ScrollToCommandData { @@ -41,7 +43,11 @@ public class ReactScrollViewCommandHelper { } public static Map getCommandsMap() { - return MapBuilder.of("scrollTo", COMMAND_SCROLL_TO); + return MapBuilder.of( + "scrollTo", + COMMAND_SCROLL_TO, + "scrollWithoutAnimationTo", + COMMAND_SCROLL_WITHOUT_ANIMATION_TO); } public static void receiveCommand( @@ -53,11 +59,18 @@ public class ReactScrollViewCommandHelper { Assertions.assertNotNull(scrollView); Assertions.assertNotNull(args); switch (commandType) { - case COMMAND_SCROLL_TO: + case COMMAND_SCROLL_TO: { int destX = Math.round(PixelUtil.toPixelFromDIP(args.getInt(0))); int destY = Math.round(PixelUtil.toPixelFromDIP(args.getInt(1))); viewManager.scrollTo(scrollView, new ScrollToCommandData(destX, destY)); return; + } + case COMMAND_SCROLL_WITHOUT_ANIMATION_TO: { + int destX = Math.round(PixelUtil.toPixelFromDIP(args.getInt(0))); + int destY = Math.round(PixelUtil.toPixelFromDIP(args.getInt(1))); + viewManager.scrollWithoutAnimationTo(scrollView, new ScrollToCommandData(destX, destY)); + return; + } default: throw new IllegalArgumentException(String.format( "Unsupported command %d received by %s.", diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java index 185394151..01ad16af8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java @@ -84,6 +84,13 @@ public class ReactScrollViewManager scrollView.smoothScrollTo(data.mDestX, data.mDestY); } + @Override + public void scrollWithoutAnimationTo( + ReactScrollView scrollView, + ReactScrollViewCommandHelper.ScrollToCommandData data) { + scrollView.scrollTo(data.mDestX, data.mDestY); + } + @Override public @Nullable Map getExportedCustomDirectEventTypeConstants() { return MapBuilder.builder()