问题:我有一个带有 TextInput 和 SectionList 的页面。当我关注 TextInput,然后单击 SectionList 中的 ListItem 时,它仅隐藏键盘,但不会导航到 SecondPage,而它应该在 Press 时执行此操作,如下所示。如果 TextInput 未聚焦 - 一切正常。
期望的行为:在关注 TextInput 时,如果我单击 ListItem - 它应该将我导航到 SecondPage。
问题:
1. 我的代码有问题还是这是SectionList 的正常行为?
2. 有没有办法实现预期的行为?
代码:
SectionList + TextInput 组件
const ds = [
{ data: [ {word: 'BMW'}, {word: 'Mercedez'}, {word: 'Tesla'}], title: 'Cars' },
{ data: [{word: 'Finland'}, {word: 'USA'}, {word: 'Somalia'}], title: 'Countries' },
{ data: [{word: 'Water'}, {word: 'Coke'}, {word: 'Tea'}], title: 'Liquids' },
];
class FirstPage extends React.Component {
render() {
return (
<View>
<View style={{ flexDirection: 'row' }}>
<TextInput
onChangeText={(v) => this.setState({ text: v})}
style={{ borderWidth: 1, flex: 1 }}
value={this.state.text}
/>
</View>
<SectionList
renderItem={({ item }) => <ListItem word={item.word} navigation={this.props.navigation} />}
renderSectionHeader={({ section }) => <ListItem word={section.title} section/>}
sections={ds}
keyExtractor={(item) => item.word}
/>
</View>
);
}
}
ListItem 组件
render() {
const { navigation, section, word } = this.props;
const { letterStyle, noteStyle, sectionStyle, termStyle } = styles;
if (section)
{
return (
<TouchableOpacity>
<View>
<CardSection style={sectionStyle}>
<Text style={letterStyle}>{word}</Text>
</CardSection>
</View>
</TouchableOpacity>
);
}
return (
<TouchableOpacity
onPress={() => navigation.navigate('SecondPage')}
>
<View>
<CardSection style={sectionStyle}>
<Text numberOfLines={1} style={termStyle}>{word}</Text>
</CardSection>
</View>
</TouchableOpacity>
);
}
最佳答案
你尝试过这个吗:
onMenuPress(item){
console.log(item);
this.props.navigation.navigate("some_route");
}
render() {
.....some code
<ListItem onPress={(item) => this.onMenuPress(item)}>
....more code
}
记住在构造函数上绑定(bind) onMenuPress 方法,如下所示:
constructor(props){
super(props);
this.onMenuPress = this.onMenuPress.bind(this);
}
对我有用。
关于javascript - 焦点位于 TextInput 时无法单击 SectionList 的 ListItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46582976/