版本:
react-native: 0.49.5
native-base: ^2.3.3
结构:
1 <Content> 2 3 <ScrollView ref={(view) => { this.myScrollView = view; }}> 4 5 <View onLayout={event => this.myView = event.nativeEvent.layout.y}> 6 7 .... 8 9 </View> 10 11 </ScrollView> 12 13 </Content>
后来发现,想通过this.myScrollView.scrollTo({ x: 0, y: this.myView, animated: true}); 无法实现跳转。
然后就上网各种查,最后在https://github.com/GeekyAnts/NativeBase/issues/105找到了答案
大概意思就是因为Content自身是一个KeyboardAwareScrollView,所以滚动事件不发生在ScrollView上,而是在Content自身上。因此,在Content中不需要用什么ScrollView。
最后的解决方案如下:
结构:
<Content ref={content => this._content = content}> <View onLayout={event => this.myView = event.nativeEvent.layout.y}> .... </View> </Content>
滚动调用的时候就用下面的方法:
this._content._root.scrollToPosition(0, this.myView, false); 第三个参数是animated,意思就是滚动到没有动画的指定位置。