Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android ScrollView: Add scrollBy method #15610

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Libraries/Components/ScrollResponder.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,21 @@ var ScrollResponderMixin = {
);
},

/**
* A helper function to scroll by a specific offset in the ScrollView.
* This is useful when you want to change the size of the content view and scroll
* position at the same time. Syntax:
*
* scrollResponderScrollBy(options: {deltaX: number = 0; deltaY: number = 0; animated: boolean = true})
*/
scrollResponderScrollBy: function(options : {deltaX?: number, deltaY?: number, animated?: boolean}) {
UIManager.dispatchViewManagerCommand(
this.scrollResponderGetScrollableNode(),
UIManager.RCTScrollView.Commands.scrollBy,
[options.deltaX || 0, options.deltaY || 0, options.animated !== false],
);
},

/**
* Deprecated, do not use.
*/
Expand Down
13 changes: 13 additions & 0 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,19 @@ const ScrollView = createReactClass({
});
},

/**
* Scrolls by a given offset, either immediately or with a smooth animation.
*
* Syntax:
*
* `scrollBy(options: {deltaX: number = 0; deltaY: number = 0; animated: boolean = true})`
*/
scrollBy: function(options: { deltaX?: number, deltaY?: number, animated?: boolean } ) {
const data = {deltaX: options.deltaX || 0, deltaY: options.deltaY || 0,
animated: options.animated !== false};
this.getScrollResponder().scrollResponderScrollBy(data);
},

/**
* Deprecated, use `scrollTo` instead.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ public void scrollToEnd(
}
}

@Override
public void scrollBy(
ReactHorizontalScrollView scrollView,
ReactScrollViewCommandHelper.ScrollByCommandData data) {
int x = scrollView.getScrollX() + data.mDeltaX;
int y = scrollView.getScrollY() + data.mDeltaY;
if (data.mAnimated) {
scrollView.smoothScrollTo(x, y);
} else {
scrollView.scrollTo(x, y);
}
}

/**
* When set, fills the rest of the scrollview with a color to avoid setting a background and
* creating unnecessary overdraw.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public class ReactScrollViewCommandHelper {
public static final int COMMAND_SCROLL_TO = 1;
public static final int COMMAND_SCROLL_TO_END = 2;
public static final int COMMAND_FLASH_SCROLL_INDICATORS = 3;
public static final int COMMAND_SCROLL_BY = 4;

public interface ScrollCommandHandler<T> {
void scrollTo(T scrollView, ScrollToCommandData data);
void scrollToEnd(T scrollView, ScrollToEndCommandData data);
void flashScrollIndicators(T scrollView);
void scrollBy(T scrollView, ScrollByCommandData data);
}

public static class ScrollToCommandData {
Expand All @@ -53,14 +55,28 @@ public static class ScrollToEndCommandData {
}
}

public static class ScrollByCommandData {

public final int mDeltaX, mDeltaY;
public final boolean mAnimated;

ScrollByCommandData(int deltaX, int deltaY, boolean animated) {
mDeltaX = deltaX;
mDeltaY = deltaY;
mAnimated = animated;
}
}

public static Map<String,Integer> getCommandsMap() {
return MapBuilder.of(
"scrollTo",
COMMAND_SCROLL_TO,
"scrollToEnd",
COMMAND_SCROLL_TO_END,
"flashScrollIndicators",
COMMAND_FLASH_SCROLL_INDICATORS);
COMMAND_FLASH_SCROLL_INDICATORS,
"scrollBy",
COMMAND_SCROLL_BY);
}

public static <T> void receiveCommand(
Expand Down Expand Up @@ -88,6 +104,14 @@ public static <T> void receiveCommand(
viewManager.flashScrollIndicators(scrollView);
return;

case COMMAND_SCROLL_BY: {
int deltaX = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(0)));
int deltaY = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(1)));
boolean animated = args.getBoolean(2);

viewManager.scrollBy(scrollView, new ScrollByCommandData(deltaX, deltaY, animated));
return;
}
default:
throw new IllegalArgumentException(String.format(
"Unsupported command %d received by %s.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ public void scrollToEnd(
}
}

@Override
public void scrollBy(
ReactScrollView scrollView,
ReactScrollViewCommandHelper.ScrollByCommandData data) {
int x = scrollView.getScrollX() + data.mDeltaX;
int y = scrollView.getScrollY() + data.mDeltaY;
if (data.mAnimated) {
scrollView.smoothScrollTo(x, y);
} else {
scrollView.scrollTo(x, y);
}
}

@Override
public @Nullable Map getExportedCustomDirectEventTypeConstants() {
return createExportedCustomDirectEventTypeConstants();
Expand Down