【问题标题】:How to change color of an component when its pressed in React Native FlatList在 React Native FlatList 中按下组件时如何更改组件的颜色
【发布时间】:2019-06-03 07:20:50
【问题描述】:

我一直在尝试开发一个列表,其中包含来自数据源的卡片和列表项。我已成功调出列表,但我想要实现的是,当用户触摸列表中的某个项目时,该项目的颜色应该会改变。

最重要的是只应选择一项。如何做到这一点?我已经通过使用 redux 操作和 reducer 来获取数据的价值。但是,我不知道如何实现这个选择过程。

我的 flatList 代码:

 <FlatList
    horizontal={true}
    data={this.qtyList}
    keyExtractor={item => item.id.toString()}
    showsHorizontalScrollIndicator={false}
    renderItem={({ item }) => (
            <TouchableHighlight 
            onPress={() => {

            }}
            >
            <Card
            containerStyle={{  borderRadius: 5 }}
            >
            <Text>
            {item.qty}
            </Text>
            </Card>
        </TouchableHighlight>
    )}
/>

请提供分步说明,因为我完全是初学者。我不想在 redux 的帮助下做到这一点,所以组件级状态会很有帮助。

【问题讨论】:

    标签: javascript react-native jsx


    【解决方案1】:

    你应该把选中项的id存储在状态中:

    <TouchableHighlight 
      onPress={() => {
        this.setState({ itemSelected: item.id }) <== your item must have a unique id
      }}
    >
    

    然后,例如在您的卡片组件中,您可以这样做:

    <Card
      containerStyle={{
        borderRadius: 5,
        backgroungColor: this.state.itemSelected === item.id ? 'red', 'white',
      }}
    >
    

    此外,您必须将extraData={this.state} 添加到您的平面列表中。 Here's the link to the doc

    【讨论】:

    • 现在所有元素都变成了蓝色,并没有像预期的那样变化
    • 已编辑答案,它应该与 extraData 道具一起使用。
    • 哇,工作太棒了!它是一个救生员!非常高兴,非常感谢兄弟!
    • 这两种解决方案实际上都解决了我的问题,感谢您抽出时间来解惑!
    【解决方案2】:

    您需要一个在用户单击 FlatList 项目时设置状态的函数。当状态改变时,组件样式将改变以显示所选项目。并且您应该将 extraData 设置为 FlatList 以便在状态更改时进行渲染。

    class Second extends React.Component {    
        constructor(props) {
            super(props);
            this.state = {
                selectedItem: null
            };
        }
    
        onPressHandler(id) {
            this.setState({selectedItem: id});
        }
    
        render() {
            return (
                <View>
                    <FlatList
                        extraData={this.state.selectedItem} //Must implemented
                        horizontal={true}
                        data={qtyList}
                        keyExtractor={item => item.id.toString()}
                        showsHorizontalScrollIndicator={false}
                        renderItem={({item}) => (
                            <TouchableOpacity
                                onPress={() => this.onPressHandler(item.id)}>
                                <Card
                                    containerStyle={this.state.selectedItem === item.id ? {
                                        borderRadius: 5,
                                        backgroundColor: "#000000"
                                    } : {borderRadius: 5, backgroundColor: "#a1a1a1"}}>
                                    <Text>{item.qty}</Text>
                                </Card>
                            </TouchableOpacity>
                        )}
                    />
                </View>
            );
        }
    }
    

    【讨论】:

    • 是否适用于单选??如果用户选择另一张卡怎么办?之前选择的卡片颜色会变为默认颜色吗?
    • 这两种解决方案实际上都解决了我的问题,感谢您抽出时间来解惑!
    猜你喜欢
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多