Javascript 想要在react native中自动滚动平面列表

Javascript 想要在react native中自动滚动平面列表,javascript,android,reactjs,react-native,react-native-flatlist,Javascript,Android,Reactjs,React Native,React Native Flatlist,我正在尝试自动滚动我的平面列表,但当我运行代码时,我无法自动滚动,如果我想手动滚动,则每隔5秒就会滚动到索引0。。 这是我的平面列表的全部代码及其refs函数 在构造函数中 this.flatList1=null; 在组件中,将安装 componentWillMount(){ setInterval(()=>{ if(this.flatList1!==null){ this.flatList1.scrollToOffset({ offset: 1 })

我正在尝试自动滚动我的平面列表,但当我运行代码时,我无法自动滚动,如果我想手动滚动,则每隔5秒就会滚动到索引0。。 这是我的平面列表的全部代码及其refs函数

在构造函数中

this.flatList1=null;
在组件中,将安装

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>
  }
/>
componentWillMount(){
设置间隔(()=>{
if(this.flatList1!==null){
this.flatList1.scrollToOffset({offset:1})
}
}, 5000);
}
{this.flatList1=flatList1}}
renderItem={({item})=>
{item.vd_title}
}
/>

您可以在此处阅读答案[]

scrollToIndex=(索引,动画)=>{
this.listRef&&this.listRef.scrollToIndex({index,animated})
}
componentDidMount(){//请改用componentDidMount,因为WillMount很快就会被弃用
setInterval(函数(){
const{sliderIndex,maxsloider}=this.state
设nextIndex=0
如果(sliderIndex

实际上,您应该增加索引,而不是像上面代码中那样将其设置为每5000ms增加1。保留currentIndex和maxIndex,并在像上面那样递增currentIndex之后使用scrollToIndex函数。请确保您在使用setInterval更新currentIndex后正在修改状态

@manahor reddy这是解决方案吗?不,我只是正确地格式化了问题谢谢您..您有任何解决方案吗?不,我不知道java脚本我来自安卓。好的,但无论如何,谢谢我应该在哪里编写scrollToIndex=(索引,动画)=>{this.listRef&&this.listRef.scrollToIndex({index,animated}})在构造函数中??它只是一个函数。将它放在类中。而不是构造函数中。在构造函数和componentDidMount之间
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)
 }