Spacing before and after a horizontal FlatList (React Native)
Asked Answered
H

7

28

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!

Hydrograph answered 9/11, 2018 at 8:20 Comment(0)
H
52

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

Hydrograph answered 9/11, 2018 at 8:46 Comment(1)
if you are looking for the prop in the docs you won't find it on FlatList. FlatList inherits ScrollView's props so you'll find it thereCater
C
22
contentContainerStyle={{paddingBottom:xxx}} 
Corfam answered 30/1, 2020 at 13:36 Comment(2)
Yes, Kamlesh, You are right this will work. As here Style paddingRight/paddingBottom will only apply to the content inside FlatList. contentContainerStyle={{paddingRight: xxx}}Reaganreagen
That's what worked for me. It only works with a number as value. A string like "25px" wont work.Neu
C
12

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>
Comprehensive answered 9/11, 2018 at 8:46 Comment(3)
Interesting approach. I was able to solve it by just using a different prop (contentContainerStyle), but I appreciate the response, upvoted!Hydrograph
Thanks.. i must admit your approach is probably cleaner. Mine is more like a workaround. But if its working then its good :)Comprehensive
I tried this - it's a great idea but the view is lost on rerendering of the listRevisionist
C
5

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 }}

/>
Calandracalandria answered 14/1, 2020 at 15:44 Comment(0)
C
3
<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>
  )}
/>
Cappuccino answered 29/4, 2020 at 1:39 Comment(0)
N
3

I use this approach to solve the problem:

 ItemSeparatorComponent={() => <View style={{ width: 35 }} />}
Nonobedience answered 7/8, 2021 at 23:54 Comment(0)
A
0

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

contentContainerStyle={{paddingRight:20}} 
Artieartifact answered 24/4, 2023 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.