Spacing before and after a horizontal FlatList (React Native)

41.5k views Asked by At

I'm trying to create a horizontal FlatList that has some spacing around it. I was able to get the beginning spacing correct with paddingLeft on the list, but paddingRight on the list doesn't seem to put any space after it (if I scroll all the way to the end, the last item is pressed right against the border).

Here is a Snack so that you can run and try this out live: https://snack.expo.io/@saadq/aW50cm

And here is my code:

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;

Using marginRight instead of paddingRight does seem to give the expected spacing result, however it causes a different issue where that whitespace is ALWAYS there and cuts off the items when scrolling. Any help would be appreciated!

7

There are 7 answers

1
Saad On BEST ANSWER

Seems like I was able to fix it by using a contentContainerStyle prop on the FlatList instead of passing it a style prop directly.

3
Nino9612 On

You could use "ListFooterComponent". Its a prop of the Flatlist and acts as the last component as the Flatlist. So you could pass a empty view with a width of for example 15 to it to get the right margin to work. Try this:

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

The important line of code is this:

ListFooterComponent={<View style={{width:15}}></View>
2
kamalesh biswas On
contentContainerStyle={{paddingBottom:xxx}} 
0
jailson pacagnan On

I use this approach to solve the problem:

 ItemSeparatorComponent={() => <View style={{ width: 35 }} />}
0
AN German On
<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>
  )}
/>
0
Rody Molenaar On

You can adjust the contentInsets for FlatList, SectionList, ScrollView with the contentInsets property. https://facebook.github.io/react-native/docs/scrollview#contentinset

E.g.

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

/>
0
Abhith On

Use 'contentContainerStyle' instead of 'style' to pass the styling. Also pass the styles as inline styles.

contentContainerStyle={{paddingRight:20}}