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

时间:2018-11-09 08:20:51

标签: react-native flexbox react-native-flatlist react-native-flexbox

我正在尝试创建一个水平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确实可以提供预期的间距结果,但是这会引起另一个问题,即空格始终存在,并且在滚动时会切断项目。任何帮助将不胜感激!

6 个答案:

答案 0 :(得分:5)

contentContainerStyle={{paddingBottom:xxx}} 

答案 1 :(得分:3)

您可以使用“ ListFooterComponent”。它是Flatlist的道具,并作为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>

答案 2 :(得分:2)

似乎我能够通过在contentContainerStyle上使用FlatList道具而不是直接通过style道具来修复它。

答案 3 :(得分:1)

您可以使用contentInsets属性调整FlatList,SectionList,ScrollView的contentInsets。 https://facebook.github.io/react-native/docs/scrollview#contentinset

例如

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

/>

答案 4 :(得分:0)

{{1}}

答案 5 :(得分:0)

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

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