【问题标题】:React Native ScrollView scrollToEnd({duration: 5000}) not working - AndroidReact Native ScrollView scrollToEnd({duration:5000})不起作用 - Android
【发布时间】:2019-11-22 10:44:51
【问题描述】:

我正在尝试制作一个 ScrollView,它会在一段时间内自动滚动到底部。但它会立即滚动到底部。

moveScreen = () =>{
this.scrollViewRef.flashScrollIndicators();
this.scrollViewRef.scrollToEnd({duration: 5000, animated: true});}

setScrollViewRef = (element) => {
this.scrollViewRef = element;};

render(){
return (
  <View style={{height:'100%', width:'100%',flexDirection:'row'}}>
    <ScrollView ref={this.setScrollViewRef} style={{height:'100%',width:'90%'}} onContentSizeChange={()=> this.moveScreen()}>
      {balloonList}
    </ScrollView>
    <View style={{height:'100%',width:'10%',justifyContent:'flex-end',}}>{bucketList}</View>
  </View>
);}

【问题讨论】:

    标签: react-native react-native-android react-native-scrollview


    【解决方案1】:

    如果您在 iOS 设备上测试此实现的功能,则无法添加 duration 选项,并且无论传递什么值,它都会立即滚动到末尾。

    对于 Android,您可以指定持续时间,例如scrollToEnd({duration: 500}) 用于受控持续时间滚动。

    有关更多信息,请参阅文档:https://reactnative.dev/docs/scrollview#scrolltoend

    编辑:

    事实证明,这个功能实际上已经准备好发布,但最终破坏了一些内部调用站点并最终被还原,但文档仍然提到它。

    https://github.com/facebook/react-native/pull/22884#issuecomment-510846680

    【讨论】:

      【解决方案2】:

      不如尝试使用 setTimeout 和你想要的持续时间,我已经做到了。

      setTimeout(() => this.scrollViewRef.scrollToEnd({duration: 500, animated: true}) , 1000);
      

      这里 1000 毫秒是我的时间,你可以提供任何时间。希望清楚,否则有任何疑问。

      【讨论】:

      • 它在 1 秒后滚动到结束(因为超时),但在那之后它会立即向下滚动。我希望滚动到底部总共需要 5 秒。
      • 你能试试 5000 的持续时间吗?500 比 1 秒还短。所以用 5000 替换 500。
      • 我这样做了,但它不起作用。我也编辑了这个问题。