【问题标题】:Change background of list item更改列表项的背景
【发布时间】:2017-09-19 13:21:51
【问题描述】:

当多个列表项被选中时,如何更改它们的背景颜色?我正在使用 react-native-accordianreact-native-collapsible 并在内容中使用平面列表。

_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>
    );
}

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

【问题讨论】:

    标签: reactjs list react-native listitem react-native-flatlist


    【解决方案1】:

    你应该改变你的代码如下,你的类会是这样的:

      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}
            />
          ...
        );
      }
    

    【讨论】:

    • 您的代码帮了大忙。但我并不完全在渲染中调用它。正如我在上面编辑的那样。有多个项目与同一索引关联,因为它是可扩展列表。因此,如果我在特定索引处选择一个项目,则在每个列表中都会选择该特定项目。只有在我重新展开列表后才会反映更改。
    • 您要我修改答案还是解决您的问题?
    • 我的问题只解决了一半。我想我需要使用带有索引的标题来为每个列表项提供唯一的 id。你能告诉我怎么做吗?
    • 我不明白你到底是什么意思。你能解释一下吗?
    • 如果我选择一个列表的项目,每隔一个列表项目就会被选中。我在上面附上了屏幕截图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2017-09-19
    相关资源
    最近更新 更多