Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native Android ScrollView scrollTo not working

I am trying to use a horizontal ScrollView in React Native for Android, where the starting position is in the middle of the scrolling images rather than (0,0).

The scrollTo method seems to be called correctly inside componentDidMount but nothing moves in the application, it still shows as starting the scroll all the way to the left.

Since this is Android I don't have access to contentOffset props or I would set that directly, according to the documentation. Here is the code:

'use strict';  var React = require('react-native'); var {   StyleSheet,   View,   Text,   ScrollView,   Component, } = React; var precomputeStyle = require('precomputeStyle');  class Carousel extends Component {   constructor(props, context) {     super(props, context);     //this.changeContent = this.changeContent.bind(this);   }    componentDidMount() {     this.myScroll.scrollTo(100);     console.log("called DidMount");   }    render() {     return (       <View style={{flex: 1}}>         <ScrollView ref={(ref) => this.myScroll = ref}           contentContainerStyle={styles.container}           horizontal={true}           pagingEnabled={true}           showsHorizontalScrollIndicator={false}           bounces={true}           onMomentumScrollEnd={this.onAnimationEnd}         >           {this.props.children}         </ScrollView>       </View>     );   }    onAnimationEnd(e) {     console.log("curr offset: " + e.nativeEvent.contentOffset.x);   } }  var styles = StyleSheet.create({   container: {     flex: 1,     justifyContent: 'center',   },   page: {     alignItems: 'center',     justifyContent: 'center',     borderWidth: 1,   }, });  module.exports = Carousel; 
like image 314
Rob M Avatar asked Oct 19 '15 07:10

Rob M


People also ask

How do I use scrollTo in ScrollView React Native?

To scroll to top of the ScrollView with React Native, we assign a ref to the ScrollView and call scrollTo on the ref's value. to create a ref with useRef and set that as the value of the ref prop of the ScrollView . Then we add a Button that calls ref. current.

How do I make my screen scrollable in React Native?

well its pretty simple, just warp your components in <ScrollView> ... </ScrollView> and style it as per your need. if height of components together exceeds for <ScrollView> , it becomes scrollable.

How do you scroll to particular view in React Native?

If your list items are of equal height, you can use them to implement scrolling to a specific item by calling scrollTo({y:itemIndex * ROW_HEIGHT}) . Show activity on this post. I have published react-native-scroll-into-view, a library which enable "scrollIntoView" feature for React Native ScrollView.

How do I add a horizontal scroll in React Native?

In the ScrollView, we can scroll the components in both direction vertically and horizontally. By default, the ScrollView container scrolls its components and views in vertical. To scroll its components in horizontal, it uses the props horizontal: true (default, horizontal: false).


1 Answers

I had the same issue, and wasted several hours no it:

  • 1: in android, ScrollView can scroll only when its size < content's size

  • 2: in react native android, if you call ScrollView.scrollTo() in componentDidMount, it won't work, because ScrollView has a layout animation when create, you can find it in ReactScrollView.java

protected void onLayout(boolean changed, int l, int t, int r, int b) {     // Call with the present values in order to re-layout if necessary     scrollTo(getScrollX(), getScrollY()); } 

so, you must delay it after the animation

componentDidMount() {     InteractionManager.runAfterInteractions(() => {       this.myScroll.scrollTo(100);         console.log("called DidMount");     })   } 
like image 173
shigebeyond Avatar answered Oct 07 '22 19:10

shigebeyond