需求:就是横向滚动条需要滚动到指定的位置。

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

  

posted on 2018-05-03 16:22  lxy02  阅读(4705)  评论(0编辑  收藏  举报