Two columns in RN FlatList

感情迁移 提交于 2019-12-11 06:59:42

问题


How can I list 2 columns in a React Native Flatlist, e.g.:


回答1:


Just pass the prop numColumns to your FlatList component.




回答2:


You can do it in one of two ways. First the straight forward way is to create 2 FlatList columns with flex layout and distribute your data between them like so:

Assuming you have style and data defined

const style={
  container: {
    flex: 1,
    flexDirection: 'row',
    height: 400
  },
  column: {
    flex: 1,
    flexDirection: 'column'
  },
  row: {
    flexDirection: 'row'
  },
  item: {
    flex: 1
  }
}

const data = [
  { key: 'A' },
  { key: 'B' },
  { key: 'C' },
  { key: 'D' },
  { key: 'E' },
  { key: 'F' },
  { key: 'G' },
  { key: 'H' },
  { key: 'I' }
];

You can do this

render() {
  //Split the data (however you want it)
  const column1Data = data.filter((item, i) => i%2 === 0);
  const column2Data = data.filter((item, i) => i%2 === 1);

  return (
    <View style={ style.container }>

      <View style={ style.column }>
        <Text>Column 1</Text>
        <FlatList
          data={ column1Data }
          renderItem={ ({ item }) => (
            <View style={ style.item }>
              <Text>{ item.key }</Text>
            </View>
          ) }
        />
      </View>

      <View style={ style.column }>
        <Text>Column 2</Text>
        <FlatList
          data={ column2Data }
          renderItem={ ({ item }) => (
            <View style={ style.item }>
              <Text>{ item.key }</Text>
            </View>
          ) }
        />
      </View>

    </View>
  );
}

The issue there is that both lists are independent and would render a bad mobile experience. A better way would be to group your data and render a single column FlatList so your content is unified.

First you would need a function to group your data into 'rows' of data

  //The key here is grouping the data to be in one row together
  const groupData = (items, groupLen) => {
    const groups = [];
    let i = 0;

    while (i < items.length) {
      groups.push(items.slice(i, i += groupLen));
    }

    return groups;
  };

Then do this...

render() {
  const groupedItems = groupData(data, 2)

  return (
    <View style={ style.column }>

        <Text>Main Column</Text>
        <FlatList
          data={ groupedItems }
          renderItem={ ({ item }) => (
            <View style={ style.row }>
              {
                /* item is really the group of items in the row */
                item.map((singleItem, index ) => (
                  <Text style={ style.item }>{ singleItem.key }</Text>
                ))
              }
            </View>
          ) }
        />

    </View>
  );
}

You will likely need to fiddle with the styling to get it like you want, but you get the idea :)




回答3:


Pass numColumns

<FlatList 
 data={albums}
 renderItem={({item}) => <></>}
 numColumns={2}
/>



回答4:


In react-native standard style is flexDirection: 'col', which means the elements are arranged from top to bottom. To change this (right to left) you should set style to flexDirection: 'row'.

<View style={{flexDirection: 'row'}}>
  <FlatList
    data={[{key: 'a'}, {key: 'b'}]}
    renderItem={({item}) => <Text>{item.key}</Text>}
  />
  <FlatList
    data={[{key: 'a'}, {key: 'b'}]}
    renderItem={({item}) => <Text>{item.key}</Text>}
  />
</View>



回答5:


It took me a long time to figure this out, but this can in fact be achieved without using any external libraries. You will need to use negative margin in a smart way.

The negative margin will be applied in the VirtualizedList prop CellRendererComponent in order to get it to work properly on Android.

The JSX:

<View style={styles.container}>
      <FlatList
        style={styles.flatlist}
        data={data}
        keyExtractor={(item, index) => index.toString()}
        CellRendererComponent={({ children, item, ...props }) => {
            return (
                <View {...props} style={{ marginTop: item.marginTop }}>
                    {children}
                </View>
            )
        }}
        renderItem={({ item }) => {
            const { source: source1, height: height1, marginTop: marginTop1 } = item.image1;
            const { source: source2, height: height2, marginTop: marginTop2 } = item.image2;
            return (
                <View style={Style.viewRow}>
                    <Image source={source1} style={[styles.image, { height: height1, marginTop: marginTop1 }]} />
                    <Image source={source2} style={[styles.image, { height: height2, marginTop: marginTop2 }]} />
                </View>
            )
        }}
    />
</View>

The data:

const source = { uri: 'https://placekitten.com/160/300' };

const data = [
    {
        marginTop: 0,
        image1: { source, height: 300, marginTop: 0 },
        image2: { source, height: 250, marginTop: 0 }
    },
    {
        marginTop: -50,
        image1: { source, height: 290, marginTop: 50 },
        image2: { source, height: 300, marginTop: 0 }
    },
    {
        marginTop: -40,
        image1: { source, height: 250, marginTop: 40 },
        image2: { source, height: 350, marginTop: 0 }
    }
];

The styles:

const styles = StyleSheet.create({
   container: {
      flex: 1
   },
   flatList: {
      width: '100%',
      height: '100%'
   },
   viewRow: {
      flexDirection: 'row'
   },
   image: {
      width: '50%',
      resizeMode: 'cover'
   }
});

Do note that arranging the images in the array properly is up to you - always place the taller image in the shorter side, and make sure to calculate the height difference and keep track of it. Have fun.



来源:https://stackoverflow.com/questions/48330207/two-columns-in-rn-flatlist

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