react-native - 如何在反应natvie中滚动到滚动视图的开头
问题描述
我是新来的反应本地人。我想做这样的事情。我有水平滚动视图,它有几个选项卡。因此,当用户按下选项卡时,我希望滚动视图滚动到选项卡的开头(到第一个选项卡)。那么我该如何使用 scrollTo() 方法来做到这一点。
<ScrollView horizontal showsHorizontalScrollIndicator={false} >
<FilterTab>
<Block fluid>
<P>
TAB 1
</P>
</Block>
</FilterTab>
<FilterTab>
<Block fluid>
<P>
TAB 2
</P>
</Block>
</FilterTab>
<FilterTab>
<Block fluid>
<P>
TAB 3
</P>
</Block>
</FilterTab>
<FilterTab>
<Block fluid>
<P>
TAB 4
</P>
</Block>
</FilterTab>
</ScrollView>
解决方案
您可以通过使用 aref
和scrollTo()
方法来做到这一点:
class YourComponent extends React.Component {
/*
With the ref added below, you can call scrollTo() on the scrollview
via the _myScrollView ref, as follows:
*/
scrollToTop() {
this.refs._myScrollView.scrollTo(0);
}
render() {
/* add the _myScrollView ref to your scroll view component */
return (
<ScrollView ref='_myScrollView' horizontal
showsHorizontalScrollIndicator={false} >
<FilterTab>
<Block fluid>
<P>
TAB 1
</P>
</Block>
</FilterTab>
<FilterTab>
<Block fluid>
<P>
TAB 2
</P>
</Block>
</FilterTab>
<FilterTab>
<Block fluid>
<P>
TAB 3
</P>
</Block>
</FilterTab>
<FilterTab>
<Block fluid>
<P>
TAB 4
</P>
</Block>
</FilterTab>
</ScrollView>)
}
}
推荐阅读
- javascript - react navigator react-native-sectioned-multi-select 如何转换成数组项
- python - 使用 Python3 和 SQLlite 存储字典数据
- c# - 显示对象名称,并添加到列表
- json - 在我的 Django 后端接收来自 ReactJS 的 JSON 对象
- firebase - Firebase 基于角色的授权
- python - 如何在 SqlAlchemy 中使用 pandas 进行 upsert
- python - 如何从 pandas DataFrame 列中提取美元金额
- excel - 在 64 位 Excel 中从 VBA 编写注册表
- c# - C# - 如何绘制具有 3 个变量的图表(Position[32]、ByteValue[255]、Frequency[int])
- python - python中的解析错误处理