Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 更改列表项的背景_Reactjs_List_React Native_Listitem_React Native Flatlist - Fatal编程技术网

Reactjs 更改列表项的背景

Reactjs 更改列表项的背景,reactjs,list,react-native,listitem,react-native-flatlist,Reactjs,List,React Native,Listitem,React Native Flatlist,选择多个列表项时,如何更改其背景色?我正在使用react native accorbian和react native collaxiable并在内容中使用平面列表 _renderContent(section, i, isActive) { //console.log("MY DATA---",section.time_slots); return ( <List style={inStyles.body} containerStyle={{ bord

选择多个列表项时,如何更改其背景色?我正在使用
react native accorbian
react native collaxiable
并在内容中使用平面列表

_renderContent(section, i, isActive)
{
 //console.log("MY DATA---",section.time_slots);
  return (
    <List
      style={inStyles.body}
      containerStyle={{ borderTopWidth: 0, borderBottomWidth: 0 }}>
        <FlatList
          data={section.time_slots}
          renderItem={
            ({ item,index }) =>
            (
             <ListItem
             onPress={() => this.selectSlot(item,section.date,index)}

             style = {[inStyles.list , {marginLeft : 15}, {marginRight : 5},
               {backgroundColor: (this.state.selectedItem[index]) ? 'green' : 'red'}]}
               title={`${item}`}
               containerStyle={{ borderBottomWidth: 0 }}
             />
          )
        }
          keyExtractor={item => section.date+item}
          ItemSeparatorComponent={this.renderSeparator}
          ListFooterComponent={this.renderFooter}
          />
      </List>
    );
}
\u渲染内容(第i节,i活动)
{
//console.log(“我的数据---”,节.时隙);
返回(
(
this.selectSlot(项、节、日期、索引)}
style={[inStyles.list,{marginLeft:15},{marginRight:5},
{backgroundColor:(this.state.selectedItem[index])?'green':'red'}]}
标题={`${item}`}
containerStyle={{borderBottomWidth:0}
/>
)
}
keyExtractor={item=>section.date+item}
ItemSeparatorComponent={this.renderSeparator}
ListFooterComponent={this.renderFooter}
/>
);
}

我只想使用
TouchableOpacity
动态更改列表项的样式。但是不能这样做。

您应该按如下方式更改代码,您的类将如下所示:

  contructor (props) {
    super(props)
    let selectedItemTemp = []
    for(let i=0; i<section.time_slots.length; i++) { //section.time_slots is your FlatList data
      selectedItemTemp.push(false)
    }
    this.state = {selectedItem: selectedItemTemp}
  }


  selectSlot = (item, section.date, index) => {
    let {selectedItem} = this.state
    selectedItem[index] = !selectedItem[index]
    this.setState({selectedItem})
    ... // your other codes
  }

  render() {
    return (
      ...
        <FlatList
          data={section.time_slots}
          renderItem={
            ({ item, index }) => (
              <TouchableOpacity
              onPress={() => this.selectSlot(item,section.date, index)}>
               <ListItem style = {[inStyles.list , {marginLeft : 15}, {marginRight : 5}, {backgroundColor: (this.state.selectedItem[index]) ? 'green' : 'white'}]}
                 title={`${item}`}
                 containerStyle={{ borderBottomWidth: 0 }}
               />
              </TouchableOpacity>
            )
          } {item => section.date+item}
          ItemSeparatorComponeultiple Selectiont={this.renderSeparator}
          ListFooterComponent={this.renderFooter}
        />
      ...
    );
  }
constructor(道具){
超级(道具)
让我们选择editemtemp=[]
for(设i=0;i{
让{selectedItem}=this.state
selectedItem[index]=!selectedItem[index]
this.setState({selectedItem})
…//您的其他代码
}
render(){
返回(
...
(
this.selectSlot(项、节、日期、索引)}>
)
}{item=>section.date+item}
ItemSeparator ComponentMultiple Selectiont={this.renderSeparator}
ListFooterComponent={this.renderFooter}
/>
...
);
}

您的代码帮了大忙。但我并没有在render中准确地调用它。正如我在上面编辑的那样。有多个项目与同一个索引关联,因为它是可扩展列表。因此,如果我在特定索引中选择一个项目,则在每个列表中都会选择该特定项目。只有在我重新扩展列表后,更改才会反映出来。是否继续你想让我修改我的答案或者你的问题解决了?我的问题只解决了一半。我想我需要使用带有索引的标题为每个列表项提供一个唯一的id。你能告诉我怎么做吗?我不太明白你的确切意思。你能解释一下吗?如果我选择一个列表项,则所有其他列表项都会被选择。我附上了上面的屏幕截图