简体   繁体   English

平面列表中的 ReactNative 复选框

[英]ReactNative checkbox inside flatlist

I have checkboxes inside Flatlist like this我在 Flatlist 中有这样的复选框

constructor(props) {
  super(props);
    this.state = { 
      checked: false
    }
 }
 breakfastData = ({item}) => {
   return(
        <ListItem style={{flex:1 , flexDirection: 'row' , justifyContent:'space-between'}} >
          <Text>{item}</Text>
          <CheckBox 
              checked={this.state.checked}
              onPress={() => this.setState({checked: !this.state.checked})}  style={{ alignSelf: 'flex-end'}} color="#FC7B04" />
        </ListItem>
        )
 }
 render(){
   return(
        <View>
          <FlatList
            data={this.state.breakfast}
            renderItem={ this.breakfastData }
            keyExtractor={(item, index) => index}
          />
        </View>
        )
 }

here is a screenshot from the app but the checkboxes don't work when i click any of the check boxes i just want the user feel that the check box is checked这是应用程序的屏幕截图,但是当我单击任何复选框时复选框不起作用我只是希望用户觉得复选框已被选中在此处输入图像描述

You must set checked for each item.您必须为每个项目设置检查。

      constructor(props) {
        super(props);
        this.state = { 
          breakfast:[
          {id:1,checked:false},
          {id:2,checked:false}
          ]
        }
      }
onItemPress = (item, index) => {
    var tempItem = item
    tempItem.checked = !item.checked
    const tempArr = [...this.state.breakfast]
    tempArr[index] = tempItem
    this.setState({
      breakfast: tempArr
    })
  }
              breakfastData = ({item}) => {
               return(
              <ListItem style={{flex:1 , flexDirection: 'row' , justifyContent:'space-between'}} >
                <Text>{item}</Text>
                <TouchableOpacity  onPress={() => {this.onItemPress(item,index)}}>
                       <CheckBox  checked={item.checked}  style={{ alignSelf: 'flex-end'}} color="#FC7B04" />
                </TouchableOpacity>
              </ListItem>
            )
          }
        render(){
        return(
        <View>
                        <FlatList
                          data={this.state.breakfast}
                          renderItem={ this.breakfastData }
                          extraData ={ this.state}
                          keyExtractor={(item, index) => index}

                        />
        </View>
        )
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM