【问题标题】:Can't click on the ListItem of SectionList while having focus in TextInput在 TextInput 中获得焦点时无法单击 SectionList 的 ListItem
【发布时间】:2017-10-05 10:04:48
【问题描述】:

问题:我有一个带有 TextInput 和 SectionList 的页面。当我专注于 TextInput 然后单击 SectionList 中的 ListItem 时 - 它只隐藏键盘但不导航到 SecondPage,它应该在 onPress 上执行,如下所示。如果 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>
            );
    }

【问题讨论】:

标签: javascript reactjs react-native react-navigation


【解决方案1】:

你试过了吗:

onMenuPress(item){
   console.log(item);
   this.props.navigation.navigate("some_route");
}

render() {
.....some code
    <ListItem onPress={(item) => this.onMenuPress(item)}>
....more code
}

记得在构造函数上绑定 onMenuPress 方法,像这样:

constructor(props){
   super(props);
   this.onMenuPress = this.onMenuPress.bind(this);
}

为我工作。

【讨论】:

    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2011-04-13
    相关资源
    最近更新 更多