繁体   English   中英

react-native SectionList如何通过使用scrollToLocation来scrollToTop?

[英]react-native SectionList How to scrollToTop by using scrollToLocation?

我只能使用此功能来滚动至item或itemTitle,但找不到找到scrollToTop的方法。 我可以在下面使用5个参数。

this.sectionListRef.scrollToLocation({
  sectionIndex: 0,
  itemIndex: -1,
  viewPosition: 1,
  viewOffset: 0,
  animated: true,
});

谢谢。

我无法使用SectionList找到任何API或func来scrollToTop。 我只能将viewOffset计算为scrollToTop。

this.sectionHeaderHeightthis.sectionHeaderHeight的高度(例如,native.layout.height)

this.sectionListRef.scrollToLocation({
  sectionIndex: 0,
  itemIndex: 0,
  viewPosition: 0,
  viewOffset: this.sectionHeaderHeight,
  animated: true,
});

添加一个功能以滚动到顶部:

scrollToTop() {
    this.scroll.scrollTo({x:0, y:0, animated: true})
}

在您的ScrollView中添加:

<ScrollView
    ref={(f) => { this.scroll = f }}
>
.......
</ScrollView>

之后,您可以在其中添加一个调用scrollToTop函数的浮动按钮。 或者,您甚至可以在设置状态或其他条件后使用它。 例如:

setSomeState(something) {
    this.setState({ something }, () => this.scroll.scrollTo({x:0, y:0, animated: true }));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM