javascript - React Native FlatList 键盘ShouldPersistTaps 不持久
问题描述
我有一个非常令人沮丧的情况。试图让键盘消失并检测子行中的 onPress 事件处理程序。
这是我的代码的样子:
_renderRow = (prediction) => {
return (
<TouchableOpacity onPress={() => {
Keyboard.dismiss();
this.setState({ location: prediction.description });
}}>
<View style={styles.listItemContainer}>
<Text>{prediction.description}</Text>
</View>
</TouchableOpacity>
)
}
render() {
return (
<View style={styles.wrapper}>
{/* style={[this.state.predictions.length > 0 ? styles.searchContainerSuggest : styles.searchContainer]} */}
<View style={styles.searchContainerSuggest}>
<View style={{paddingLeft: 10, height: 45, display: 'flex', justifyContent: 'center'}}>
<TextInput
placeholder="Enter location"
value={this.state.location}
onChangeText={location => this.onChangeLocation(location)}
style={styles.textInput}
/>
</View>
{this.state.predictions.length && this.state.location !== '' ?
<FlatList
keyboardShouldPersistTaps={'handled'}
refreshing={!this.state.loaded}
initialNumToRender={10}
enableEmptySections={true}
data={this.state.predictions}
keyExtractor={(_, index) => index.toString()}
renderItem={ ({item: prediction}) => this._renderRow(prediction) } />
: null}
</View>
</View>
);
}
关于如何调试此问题,我可能需要一两个帮助。
查找了几个有关如何处理隐藏键盘并允许同时按下特定选择的示例。
我认为keyboardShouldPersistTaps 将允许选择子选项。选择后,onPress 事件处理程序将触发,这将是我调用 Keyboard.dismiss() 以隐藏键盘的地方。似乎不起作用。
解决方案
在我的情况下,除了添加keyboardShouldPersistTabs='handled'
到有FlatList
问题的内容之外,还需要添加keyboardShouldPersistTabs='handled'
和添加nestedScrollEnabled={true}
到上面 2 个级别的父ScrollView
级,包装FlatList
我打算使用此行为。在repo 中查看此问题react-native
以获取更多信息。
推荐阅读
- php - 为什么 CodeIgniter 4 的 anchor() 使用 site_url() 格式(带 indexPage)而不是 base_url()(不带 indexPage)?
- kubernetes - 本地 Kuber 集群上的 Ingress 与直接 Nginx 部署
- rust - 在 Cargo.toml 中定义货物配置文件的特征
- reverse-engineering - 找不到绘图者 'qwingraph.exe' ida
- javascript - 交互式弹出/空白
- kotlin - 复用关闭的 Kotlin 的 Channel
- qt - 在 Visual Studio 2017 中使用和安装 libVlc 或 GStreamer for QT
- vb.net - 我想使用 Regex.IsMatch 在 VB.net Visual Basic 中查找 TB、GB、MB
- forms - 在 vue 中重置验证
- mysql - 无法使用 psycopg2 在 postgres 中创建临时表