23

我有一个包含多列的 FlatList:

    <FlatList
      numColumns={4}
      ItemSeparatorComponent={this.renderSeparator}
      ...
    </FlatList>

和一个行分隔符:

  renderSeparator = () => (
    <View
      style={{
        backgroundColor: 'red',
        height: 0.5,
      }}
    />
  );

但是分隔符只出现在行之间,而不是列之间(即使我添加 width: 0.5

看世博

4

7 回答 7

30

您可以在 renderItems 中添加 if else 条件并检查索引模数以添加分隔符。我只使用这个,一切都很好。

就像是 :-

_renderItem = ({item,index}) => {
   return(
      <View style={[{ backgroundColor: 'red', flex: 1 }, index%2==0 ? { marginRight: 10 } : { marginLeft: 10 } ]}>
         <Text>{item.key}</Text>
      </View>
   )
}

希望这会帮助你。

于 2018-01-06T06:09:23.093 回答
2

我有点参加聚会,但我遇到了同样的问题,并通过使用这个 renderRow 代码解决了这个问题。我在网格视图中有 2 列。请通过在索引 % X === 0索引 <= Y中替换 X 来更改列长度,其中 Y 是 columns-1

renderRow({ item, index }) {
    return (
      <View style={[styles.GridViewContainer, 
                    index % 2 === 0 ? {
                      borderLeftWidth: 1, borderLeftColor: 'black',
                      borderRightWidth: 1, borderRightColor: 'black',} 
                      : 
                      {borderRightWidth: 1, borderRightColor: 'black'}

                      ,{borderBottomWidth: 1, borderBottomColor: 'black'}
                      ,index <= 1 && {borderTopWidth: 1, borderBottomColor: 'black'}
                    ]}
      >

      ... render item code ...
        </View>
    )
  }
于 2019-12-29T12:19:56.617 回答
1

万一将来有人遇到这种情况,我发现了使用 flexbox 的替代方法。

FlatList 接受 columnwrapperStyle 所以 justifyContent: 'space-around' 的风格会做

然后对于在 renderItem 中返回的元素,给出一个可被 1 整除的 flex,因此如果 numColumns 为 2,您可以将 renderItem 的 flex 设置为大约 0.45

<FlatList
numColumns={2}
ItemSeparatorComponent={() => (
              <View style={{ backgroundColor: "green", height: 2 }} />
            )}
 columnWrapperStyle={{
       justifyContent: "space-between",
     }}
renderItem={({item, index}) => <View style={{flex: 0.45, height: 120, backgroundColor: '#dedede' }}>
<Text>{index}</Text>
</View>}
/>
于 2021-11-04T07:22:19.220 回答
1

抱歉,我也没有找到使用平面列表组件中的属性添加列分隔符的方法。所以我只是在渲染项中添加了文本组件之外的视图,如下所示:

export default class App extends Component {

 render() {
  return (
   <View style={styles.view}>
    <FlatList
      data={['1', '2', '3', '4', '5', '6', '7', '8']}
      numColumns={4}
      renderItem={({ item }) => (
        <View style={styles.separator}>
          <Text style={styles.text}>{item}</Text>
        </View>
      )}
    />
   </View>
  );
 }
}

const styles = StyleSheet.create({
 view: {
  paddingTop: 30,
 },
 text: {
  flex: 1,
  fontSize: 40,
  textAlign: 'center'
 },
 separator: {
  flex: 1, 
  borderWidth: 1, 
  borderColor: 'red'
 },
});

这是结果:

上述示例代码的结果。

我希望这可以帮助你。:)

于 2017-08-09T06:02:57.110 回答
0

我看了你的世博会。如下。

 1   2   3   4 
---------------
 5   6   7   8 

我假设你想要如下。

 1 | 2 | 3 | 4 
---+---+---+---
 5 | 6 | 7 | 8 

要做到这一点,这是不可能的ItemSeparatorComponent。正如 Hazim Ali 所说,每个索引使用不同的样式。

renderItem={({ item, index }) => (
        <Text style={[styles.text, index % 4 !== 0 && {borderLeftWidth: 1, borderLeftColor: 'red'}]}>{item}</Text>
    )}

在此处输入图像描述

这是完整的例子

--

但是分隔符只出现在行之间,而不是列之间

据我阅读源代码,当 numColumns > 2 时,列之间没有项目分隔符。Itemseparator 仅在行和行之间。

这是示例。当 numColumns 设置为 4 时,四个项目被分组到一个单元格中。并且在单元格之间放置了一个 itemSeparator。

 1   2   3   4  <- cell1
--------------- <- itemSeparator
 5   6   7   8  <- cell2
于 2018-01-06T12:59:28.450 回答
0

您可以在您的 Text 组件周围添加一个 View 包装器并将borderRight 样式应用于 View 组件,请参见此处的示例: https ://snack.expo.io/HJx68bKvb ,尝试在 Expo 的 Android 模拟器中运行,Expo 的一些 iOS 模拟器原因是没有正确显示边框,而是在我的本地模拟器上工作。

您可以在 View 组件和 Text 组件上使用填充来获得所需的边框位置。

于 2017-08-09T05:15:21.253 回答
-1

您可以为每个项目提供边距,并为容器提供负边距。这是 CSS flex 布局的一个非常常见的技巧。

  renderItem = (sale) => {
    const {item, index} = sale;
    return (
      <View style={{flex:1, backgroundColor:'#f00', margin:20}}>  ### LOOK AT HERE ###
      </View>
    )
  };

  render() {
    return (
    <View style={{flex:1,}} >
      <FlatList
        style={{ margin:-20}}   ### LOOK AT HERE ###
        data={this.state.sales}
        numColumns={2}
        renderItem={this.renderItem}
      />
    </View>
    )
  }

点击这里查看我的作品

于 2018-03-12T15:25:36.790 回答