React Native - SectionList numColumns支持

时间:2017-12-15 13:41:27

标签: react-native react-native-flatlist

FlatListnumColumns支持。如何使用SectionList设置numColumns

Github问题:SectionList renderItem multi item support #13192

5 个答案:

答案 0 :(得分:15)

这是我对SectionList的numColumns的解决方案。如果你最好让我知道。

class Example extends Component {
  static propTypes = {
    numColumns: PropTypes.number
  };

  static defaultProps = {
    numColumns: 2
  };

  _renderSection = data => <Section {...data} />;

  _renderItem = ({ section, index }) => {
    const { numColumns } = this.props;

    if (index % numColumns !== 0) return null;

    const items = [];

    for (let i = index; i < index + numColumns; i++) {
      if (i >= section.data.length) {
        break;
      }

      items.push(<Item item={section.data[i]} />);
    }

    return (
      <View
        style={{
          flexDirection: "row",
          justifyContent: "space-between"
        }}
      >
        {items}
      </View>
    );
  };

  render() {
    return (
      <SectionList
        sections={dumyData}
        style={styles.container}
        renderItem={this._renderItem}
        renderSectionHeader={this._renderSection}
      />
    );
  }
}

答案 1 :(得分:2)

可以将FlatList与numColumns prop一起用作SectionList的renderItem。

const data = [ //Notice [[...]] instead of [...] as in the RN docs
    {data: [[...]], title: ...},
    {data: [[...]], title: ...},
    {data: [[...]], title: ...},
]

render () {
    return (
        <SectionList
            renderItem={this._renderSectionListItem}
            renderSectionHeader={this._renderSectionHeader}
            sections={data}
        />
    )
}

renderSectionListItem = ({item}) => {
    return (
        <FlatList
            data={item}
            numColumns={3}
            renderItem={this.renderItem}
        />
    )
}

答案 2 :(得分:1)

深入研究这个问题,我提出了与Pir Shukarullah Shah相似的解决方案。

我正在使用FlatList而不是常规项目,只考虑了<SectionList/>'s renderItem方法中的第一项。

 _renderList = ({ section, index }) => {
    if (index !== 0) return null;

    return (
      <FlatList numColumns={columns}
        columnWrapperStyle={styles.container}
        data={section.data}
        renderItem={this._renderItem}
        keyExtractor={keyExtractor}
      />
    )
  }



...
      <SectionList
        renderItem={this._renderList}
        renderSectionHeader={this._renderSectionHeader}
        sections={itemList}
        keyExtractor={keyExtractor}
      />

答案 3 :(得分:0)

 const DATA = [
     {
      renderItem: ({ item, index }) => {
          return (<View style={{flexDirection:'row', alignItems:'center', justifyContent:'space-between', }}>
          {item.map((elem,index)=>(<View style={{  borderColor: 'black', borderWidth: 2, minWidth:100 }}>
            <Text>{elem.value}</Text>
          </View>))
          }
          </View>);
       },
      data: [
        [{id:'1', value:'Pizza'}, {id:'2', value:'Burger'}, {id:'3', value:'Onion Rings'}], //this array length will be noOfColumns
        [{id:'4', value:'Risotto'}, {id:'5', value:'French Fries'}, {id:'6', value:'Water'}],
      ],
     },


   <SectionList
      ref={listRef}
      sections={DATA}
      keyExtractor={_keyExtractor}
    />

答案 4 :(得分:0)

我发现有一个简单的解决方案。请尝试将以下属性添加到

contentContainerStyle={{ flexDirection : '行', justifyContent : 'flex-start', alignItems : 'flex-start', flexWrap : '包裹' }}

此外,设置和呈现宽度等于 SectionList 宽度的 Section Header。否则,列表项将显示在行方向的节标题之后。