Skip to content

Commit

Permalink
Update Android's ScrollView.scrollTo API to match JS/iOS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicklockwood authored and facebook-github-bot-9 committed Feb 1, 2016
1 parent 9308f89 commit ee30433
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 51 deletions.
18 changes: 5 additions & 13 deletions Libraries/Components/ScrollResponder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,9 @@ public void receiveCommand(

@Override
public void scrollTo(
RecyclerViewBackedScrollView view,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
view.scrollTo(data.mDestX, data.mDestY, true);
}

@Override
public void scrollWithoutAnimationTo(
RecyclerViewBackedScrollView view,
RecyclerViewBackedScrollView scrollView,
ReactScrollViewCommandHelper.ScrollToCommandData data) {
view.scrollTo(data.mDestX, data.mDestY, false);
scrollView.scrollTo(data.mDestX, data.mDestY, data.mAnimated);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ public void receiveCommand(
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,27 @@
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(
Expand All @@ -62,13 +60,8 @@ public static <T> void receiveCommand(
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,11 @@ public void receiveCommand(
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
Expand Down

0 comments on commit ee30433

Please sign in to comment.