From 235c059605c63f623b7f371db71d4182a63a828c Mon Sep 17 00:00:00 2001 From: Siqi Liu Date: Fri, 22 Jul 2016 09:14:11 -0700 Subject: [PATCH] Support API "scrollTo" in RecyclerViewBackedScrollView on Android Summary: In Android `RecyclerViewBackedScrollView` didn't provide the `scrollTo` API, however iOS does. If a ListView was created with `RecyclerViewBackedScrollView` as its `renderScrollComponent`, then calling `scrollTo` wouldn't work. This diff enables the `scrollTo` API in `RecyclerViewBackedScrollView` on Android. Reviewed By: dmmiller Differential Revision: D3605233 fbshipit-source-id: f192053361f45453e5fce3fb6038ab03ac4025af --- .../RecyclerViewBackedScrollView.android.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Libraries/Components/ScrollView/RecyclerViewBackedScrollView.android.js b/Libraries/Components/ScrollView/RecyclerViewBackedScrollView.android.js index 86e60af82..cc7246daf 100644 --- a/Libraries/Components/ScrollView/RecyclerViewBackedScrollView.android.js +++ b/Libraries/Components/ScrollView/RecyclerViewBackedScrollView.android.js @@ -75,6 +75,30 @@ var RecyclerViewBackedScrollView = React.createClass({ this.props.onContentSizeChange(width, height); }, + /** + * A helper function to scroll to a specific point in the scrollview. + * This is currently used to help focus on child textviews, but can also + * be used to quickly scroll to any element we want to focus. Syntax: + * + * scrollResponderScrollTo(options: {x: number = 0; y: number = 0; animated: boolean = true}) + * + * Note: The weird argument signature is due to the fact that, for historical reasons, + * the function also accepts separate arguments as as alternative to the options object. + * This is deprecated due to ambiguity (y before x), and SHOULD NOT BE USED. + */ + scrollTo: function( + y?: number | { x?: number, y?: number, animated?: boolean }, + x?: number, + animated?: boolean + ) { + if (typeof y === 'number') { + console.warn('`scrollTo(y, x, animated)` is deprecated. Use `scrollTo({x: 5, y: 5, animated: true})` instead.'); + } else { + ({x, y, animated} = y || {}); + } + this.getScrollResponder().scrollResponderScrollTo({x: x || 0, y: y || 0, animated: animated !== false}); + }, + render: function() { var recyclerProps = { ...this.props,