javascript - 如何滚动到 React Native 中的视图?
问题描述
我正在使用 React Native 开发一个应用程序。在那里,我想View
在按下按钮时滚动到 a 。我试过这样的事情。
render() {
return(
<View ref={field => this.myView = field} >
//something inside the view
</View>
)
}
然后,尝试
this.myView.focus()
但是,它不起作用。
我想得到如下 GIF 演示中所示的结果。这个怎么做?
解决方案
您应该存储ScrollView
ref 并使用scrollViewRef.scrollTo()
render() {
return(
<ScrollView ref={ref => this.scrollViewRef = ref}>
<View
// you can store layout for each of the fields
onLayout={event => this.layout = event.nativeEvent.layout}
>
// some field goes here
</View>
</ScrollView>
)
}
然后当发生错误时,只需滚动到您的字段this.scrollViewRef.scrollTo({ y: this.layout.y, animated: true });
。
更新: 可以在这个 repo中找到一个工作示例。
推荐阅读
- python - Pygame:使用遗传算法的点游戏
- javascript - html 菜单折叠但没有移动到指定的 id
- javascript - 在 Angular CLI 8 PrimeNG 中找不到名称“BrowserAnimationsModules”
- powershell - Get-ChildItem 和 Get-Content 对象之间的 Powershell 比较
- c# - 检测对另一个 userControl 的点击而不是当前的点击
- django - Django 中间件获取动态 url 参数值
- javascript - 将数据从 javascript 变量传递到 PHP 变量
- php - Laravel 5.4 验证失败后不重定向
- php - 我尝试使用会话数据调用数组上的成员函数 save()
- java - 如何按位数对列表中的数字进行分组