需求:就是橫向滾動條需要滾動到指定的位置。
react-native版本:0.49.3
結構:
<ScrollView horizontal={true} ref={(view) => { this.myScrollView = view; }}> <View></View> ... <ScrollView>
在這里如果給View加上ref屬性,例如ref=‘test’,然后想通過
this.refs.test.measure((fx, fy, width, height, px, py) => { this.myScrollView.scrollTo({ x: px, y: py, animated: true }); });
上面這種方法跳轉是無效的,因為px,py這些都是undefined。
只能采取如下方法:
先給View增加一個onLayout屬性:
<View onLayout={event=>{this.layoutX = event.nativeEvent.layout.x}}>
然后通過下面這樣就可以實現滾動到指定位置:
this.myScrollView.scrollTo({ x: this.layoutX, y: 0, animated: true});