首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React-Native中的ScrollToIndex

React-Native中的ScrollToIndex
EN

Stack Overflow用户
提问于 2018-06-07 00:07:45
回答 2查看 1K关注 0票数 0

在我的应用程序中,我想使用scrollToIndex(),它是FlatList组件的一个方法。

代码语言:javascript
复制
 render() {
    let { posts, featuredWorkouts, mainFeedIsLoading } = this.props;
    let mainFeed = <SwiperContainer featuredWorkouts={featuredWorkouts} />;

    let mainFeedData = (
      <View>
        <View style={{ marginBottom: 5 }}>
          <SwiperContainer featuredWorkouts={featuredWorkouts} />
        </View>

        <FlatList
          data={posts}
          scrollToIndex={5}
          extraData={[posts, mainFeedIsLoading]}
          renderItem={item => this.renderPost(item)}
          keyExtractor={item => item.shareId}
        />
      </View>
    );

    if (mainFeedIsLoading) {
      mainFeedData = (
        <View style={styles.screenLoader}>
          <ScreenLoader color="lightblue" size="large" />
        </View>
      );
    }

    return (
      <View>
        <ScrollView>{mainFeedData}</ScrollView>
      </View>
    );
  }

例如,当页面加载时,我想转到第10个索引。我如何才能做到这一点?我尝试了scrollToIndex(),但它不起作用。

EN

回答 2

Stack Overflow用户

发布于 2019-03-15 20:23:59

您必须将ref添加到您的平面列表中。

代码语言:javascript
复制
ref={(ref) => { this.list = ref; }}

此外,您还必须添加到滚动索引到您的组件。

代码语言:javascript
复制
componentDidMount() {
    this.list.scrollToIndex({animated: true, index: tempIndex, viewOffset:0,viewPosition:0});
}

您还可以使用:

代码语言:javascript
复制
initialScrollIndex
票数 1
EN

Stack Overflow用户

发布于 2019-12-22 13:41:38

在我的例子中,我必须这样做才能使properly to in正常工作,

1.添加对扁平列表的引用:

代码语言:javascript
复制
`
ref={ref => {
this.flatListRef = ref;
}}
`

带超时的

  1. 调用scrollToIndex():

代码语言:javascript
复制
`
setTimeout(() => {
      this.flatListRef.scrollToIndex({
    animated: true,
    index: this.state.pressedIndex
      });
    }, 500);
`

然后,

  1. 在flatlist

内再次调用函数

代码语言:javascript
复制
 onScrollToIndexFailed={() => 
 {
  this.scrollToIndex();

}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50724931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档