React Native FlatList with alternate rows having different number of columns

回眸只為那壹抹淺笑 提交于 2019-12-23 02:46:18

问题


i want to have a FlatList which renders a single item on odd rows and 2 items on even rows

Is it possible to achieve this layout. I apologize I do not have code for it up yet.


回答1:


FlatList has a renderItem prop which is a function. That function is called for each item which FlatList draws. You have to pass that function to FlatList. In that function, you return the View to be drawn for each item. So you can return whatever you want to draw for a particular row.

The renderItem function passes the index of the item as the second argument. In your case you can use that argument to draw 1 or two columns (or whatever else you might want to draw).

<FlatList
  data={[{key: 'a', title: 'single column stuff' }, {key: 'b', col1: 'col1 text', col2: 'col2 text'}]}
  renderItem={(item, index) => {
    if(index % 2 === 0) {
      return <View style={{flexDirection: 'row'}}>
        <View style={{flex: 1}}><Text> {item.col1} </Text></View>
        <View style={{flex: 1}}><Text> {item.col2} </Text></View>
      </View>
    } else {
      return <View style={{flex: 1}}><Text> {item.title} </Text></View>
    }
  }}

/>


来源:https://stackoverflow.com/questions/49666330/react-native-flatlist-with-alternate-rows-having-different-number-of-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!