水平FlatList前后的间距(React Native)

0s7z1bwu  于 2023-05-01  发布在  React
关注(0)|答案(7)|浏览(236)

我正在尝试创建一个水平的FlatList,它周围有一些间距。我能够在列表中使用paddingLeft来正确地获得开始的间距,但是列表中的paddingRight似乎没有在它后面放任何空格(如果我一直滚动到最后,最后一项会被压在边框上)。
这里有一个小吃,让你可以运行并尝试这个现场:https://snack.expo.io/@saadq/aW50cm
下面是我的代码:

import * as React from 'react';
import { Text, View, FlatList, StyleSheet } from 'react-native';

const data = [{ key: 1 }, { key: 2 }, { key: 3 }, { key: 4 }];

class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <FlatList
          style={styles.flatList}
          horizontal
          data={data}
          renderItem={() => (
            <View style={styles.box}>
              <Text>Box</Text>
            </View>
          )}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
  flatList: {
    marginTop: 100,
    paddingLeft: 15,
    paddingRight: 15, // THIS DOESN'T SEEM TO BE WORKING
    // marginRight: 15   I can't use marginRight because it cuts off the box with whitespace
  },
  box: {
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    height: 50,
    width: 100,
    borderWidth: 1,
    borderColor: 'black',
    paddingHorizontal: 15,
    marginRight: 15,
  },
});

export default App;

使用marginRight而不是paddingRight似乎确实给予了预期的间距结果,但是它会导致一个不同的问题,即空白始终存在并在滚动时切断项目。任何帮助将不胜感激!

3qpi33ja

3qpi33ja1#

看起来我可以通过在FlatList上使用contentContainerStyle prop 来修复它,而不是直接传递给它一个style prop 。

z4bn682m

z4bn682m2#

contentContainerStyle={{paddingBottom:xxx}}
lpwwtiir

lpwwtiir3#

您可以使用“ListFooterComponent”。它是Flatlist的一个 prop ,充当Flatlist的最后一个组件。因此,您可以将一个宽度为15的空视图传递给它,以获得正确的边距。试试这个:

<FlatList
      style={styles.flatList}
      horizontal
      data={data}
      renderItem={() => (
        <View style={styles.box}>
          <Text>Box</Text>
        </View>
      )}
      ListFooterComponent={<View style={{width:15}}></View>}

重要的一行代码是这样的:

ListFooterComponent={<View style={{width:15}}></View>
efzxgjgh

efzxgjgh4#

可以使用contentInsets属性调整FlatList、SectionList和ScrollView的contentInsets。 www.example.com
例如:

<FlatList
  data={...}
  renderItem={...}
  horizontal={true}
  contentInset={{ right: 20, top: 0, left: 0, bottom: 0 }}

/>
8cdiaqws

8cdiaqws5#

<FlatList
  // extraData={this.props.x}
  contentContainerStyle={{paddingBottom: 10, paddingTop: 8}}
  data={contacts}
  removeClippedSubviews={false}
  keyExtractor={(item, index) => index.toString()}
  // onRefresh={false}
  renderItem={({item}) => (
    <View
      style={{
        flex: 1,
        flexDirection: 'row',
        justifyContent: 'space-between',
        // margin: 3,
        marginVertical: 5,
        marginHorizontal: 4,
        // borderWidth: 1,
        // borderColor: '#d6d7da',
        borderRadius: 7,
        // padding: 1,
        // borderRightWidth: 1, borderRightColor: '#d6d7da'
      }}>
     <Text>HOLA</Text>
    </View>
  )}
/>
8xiog9wr

8xiog9wr6#

我用这个方法来解决这个问题:

ItemSeparatorComponent={() => <View style={{ width: 35 }} />}
x0fgdtte

x0fgdtte7#

使用“contentContainerStyle”而不是“style”传递样式。也将样式作为内联样式传递。

contentContainerStyle={{paddingRight:20}}

相关问题