Update Android's ScrollView.scrollTo API to match JS/iOS

Summary:
public
We recently updated the `ScrollResponder.scrollResponderScrollTo` method to accept an `animated` argument, and deprecated the `scrollResponderScrollWithoutAnimationTo` method. This change was reflected in the native iOS implementation, but not on Android.

This diff updates the Android ScrollViewManager implementation to match the JS API, and removes the platform-specific fork in the JS code.

Reviewed By: dmmiller

Differential Revision: D2883515

fb-gh-sync-id: e5a0e1cf470e21af837b2311cf1048162ac3aff5
This commit is contained in:
Nick Lockwood 2016-02-01 04:03:55 -08:00 committed by facebook-github-bot-9
parent 9308f89c1c
commit ee30433b76
5 changed files with 23 additions and 51 deletions

View File

@ -353,19 +353,11 @@ var ScrollResponderMixin = {
* can also be used to quickly scroll to any element we want to focus
*/
scrollResponderScrollTo: function(offsetX: number, offsetY: number, animated: boolean = true) {
if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
React.findNodeHandle(this),
UIManager.RCTScrollView.Commands[animated ? 'scrollTo' : 'scrollWithoutAnimationTo'],
[Math.round(offsetX), Math.round(offsetY)],
);
} else {
ScrollViewManager.scrollTo(
React.findNodeHandle(this),
{ x: offsetX, y: offsetY },
animated
);
}
UIManager.dispatchViewManagerCommand(
React.findNodeHandle(this),
UIManager.RCTScrollView.Commands.scrollTo,
[offsetX, offsetY, animated],
);
},
/**

View File

@ -75,16 +75,9 @@ public class RecyclerViewBackedScrollViewManager extends
@Override
public void scrollTo(
RecyclerViewBackedScrollView view,
RecyclerViewBackedScrollView scrollView,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
view.scrollTo(data.mDestX, data.mDestY, true);
}
@Override
public void scrollWithoutAnimationTo(
RecyclerViewBackedScrollView view,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
view.scrollTo(data.mDestX, data.mDestY, false);
scrollView.scrollTo(data.mDestX, data.mDestY, data.mAnimated);
}
@Override

View File

@ -75,13 +75,10 @@ public class ReactHorizontalScrollViewManager
public void scrollTo(
ReactHorizontalScrollView scrollView,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
scrollView.smoothScrollTo(data.mDestX, data.mDestY);
}
@Override
public void scrollWithoutAnimationTo(
ReactHorizontalScrollView scrollView,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
scrollView.scrollTo(data.mDestX, data.mDestY);
if (data.mAnimated) {
scrollView.smoothScrollTo(data.mDestX, data.mDestY);
} else {
scrollView.scrollTo(data.mDestX, data.mDestY);
}
}
}

View File

@ -25,29 +25,27 @@ 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<T> {
void scrollTo(T scrollView, ScrollToCommandData data);
void scrollWithoutAnimationTo(T scrollView, ScrollToCommandData data);
}
public static class ScrollToCommandData {
public final int mDestX, mDestY;
public final boolean mAnimated;
ScrollToCommandData(int destX, int destY) {
ScrollToCommandData(int destX, int destY, boolean animated) {
mDestX = destX;
mDestY = destY;
mAnimated = animated;
}
}
public static Map<String,Integer> getCommandsMap() {
return MapBuilder.of(
"scrollTo",
COMMAND_SCROLL_TO,
"scrollWithoutAnimationTo",
COMMAND_SCROLL_WITHOUT_ANIMATION_TO);
COMMAND_SCROLL_TO);
}
public static <T> void receiveCommand(
@ -62,13 +60,8 @@ public class ReactScrollViewCommandHelper {
case COMMAND_SCROLL_TO: {
int destX = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(0)));
int destY = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(1)));
viewManager.scrollTo(scrollView, new ScrollToCommandData(destX, destY));
return;
}
case COMMAND_SCROLL_WITHOUT_ANIMATION_TO: {
int destX = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(0)));
int destY = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(1)));
viewManager.scrollWithoutAnimationTo(scrollView, new ScrollToCommandData(destX, destY));
boolean animated = args.getBoolean(2);
viewManager.scrollTo(scrollView, new ScrollToCommandData(destX, destY, animated));
return;
}
default:

View File

@ -82,14 +82,11 @@ public class ReactScrollViewManager
public void scrollTo(
ReactScrollView scrollView,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
scrollView.smoothScrollTo(data.mDestX, data.mDestY);
}
@Override
public void scrollWithoutAnimationTo(
ReactScrollView scrollView,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
scrollView.scrollTo(data.mDestX, data.mDestY);
if (data.mAnimated) {
scrollView.smoothScrollTo(data.mDestX, data.mDestY);
} else {
scrollView.scrollTo(data.mDestX, data.mDestY);
}
}
@Override