关于react native里滚动跳转到指定位置


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

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

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM