React native Android ScrollView scrollTo not working

前端 未结 4 850
太阳男子
太阳男子 2020-12-15 17:00

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,

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 17:23

    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");
        })  
    }
    

提交回复
热议问题