javascript - 想在 native react 中自动滚动平面列表

标签 javascript android reactjs react-native react-native-flatlist

我正在尝试自动滚动我的平面列表,但是当我运行我的代码时,我无法自动滚动,如果我想手动滚动,它会在每 5 秒后进入索引 0 .. 这是我的平面列表及其 refs 函数的所有代码

在构造函数中

this.flatList1=null;

而在componentwillMount

componentWillMount(){

  setInterval(()=>{
    if(this.flatList1!==null){
      this.flatList1.scrollToOffset({ offset: 1 })
    }
  }, 5000);
}

<FlatList horizontal
  data={this.state.getallvideos}
  ref={flatList1 => { this.flatList1 = flatList1 }}
  renderItem={({item}) =>  

    <TouchableOpacity onPress={this.playvideoinnetpage.bind(this,item.vd_link,item.vd_thumbnail,item.vd_id,item.vd_title)}>

      <Card
        containerStyle={{
          padding:0, 
          width:180,
          height:112,
          backgroundColor:'#000',
          borderColor:'#000',
          marginTop:10,
          marginLeft: 5,
          marginRight:5,
          marginBottom:5
        }}
        image={{uri:item.vd_thumbnail}}
        imageStyle={'stretch'}
      >

        <View style={{position:'relative',bottom:75,}}> 
          <Text numberOfLines={1} style={{color:'#fff',fontWeight:'bold',fontSize:14}}> {item.vd_title}</Text>
        </View>
      </Card>
    </TouchableOpacity>
  }
/>

最佳答案

您可以在此处阅读答案 [ How do I make a list (FlatList) automatically scroll through the elements using Animated? ]

scrollToIndex = (index, animated) => {
   this.listRef && this.listRef.scrollToIndex({ index, animated })
 }

 componentDidMount() {  // use componentDidMount instead since the WillMount is getting deprecated soon
   setInterval(function() {
     const { sliderIndex, maxSlider } = this.state
     let nextIndex = 0

     if (sliderIndex < maxSlider) {
       nextIndex = sliderIndex + 1
     }

     this.scrollToIndex(nextIndex, true)
     this.setState({sliderIndex: nextIndex})
   }.bind(this), 3000)
 }

您基本上应该增加索引,而不是像上面的代码那样每 5000 毫秒将其设置为 1。保留 currentIndex、maxIndex 并在像上面那样递增 currentIndex 后使用 scrollToIndex 函数。请确保在使用 setInterval 更新 currentIndex 后修改状态

关于javascript - 想在 native react 中自动滚动平面列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54569618/

相关文章:

JavaScript Closure - 监听类中所有元素的事件

Android:检测应用程序何时被杀死

php - 在 ajax 加载时创建加载图像

javascript - ipad/ipod touch 上的 flowplayer 视频无法使用 ipad 插件在 drupal7 中工作 - 字节范围 header 问题

java - 使用 Jsoup 获取图片地址

android - 如何在 Android 服务中使用 SQLite?

javascript - 你如何让 ReactJS 成为 AngularJS 的 MVC 架构中的 'V'?

javascript - 如何在 React Static Boilerplate 中创建 webpack 导入别名?

reactjs - “loggedInUser”可能是 'undefined' .ts(18048)

javascript - 带有 Google 可视化的粘性页脚