FlatList component has 2 major props named as ListHeaderComponent={} and ListFooterComponent={}, they both are used to set header and footer on FlatList. The header is used to show some particular information about FlatList data and the footer is used to show any message at the end item of FlatList data like FlatList has finished here. So in this tutorial we would going to Add Show Header Footer on FlatList in Android iOS React Native App Example Tutorial.
Contents in this project Add Show Header Footer on FlatList in Android iOS React Native App Example Tutorial:
1. Import Platform, StyleSheet, View, Text, FlatList and Alert component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { Platform, StyleSheet, View, Text, FlatList, Alert } from ‘react-native’;
|
2. Create constructor() in your project, Now make a state named as FlatListItems. It is used to add items data in FlatList.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
constructor()
{
super();
this.state = {
FlatListItems: [
{key: ‘Apple’},
{key: ‘Apricot’},
{key: ‘Avocado’},
{key: ‘Banana’},
{key: ‘Blackberry’},
{key: ‘Blackcurrant’},
{key: ‘Blueberry’},
{key: ‘Boysenberry’},
{key: ‘Cherry’}
]}
}
|
3. Create a function named as FlatListItemSeparator, This would used to render a Horizontal line between each FlatList item.
1
2
3
4
5
6
7
8
9
10
11
|
FlatListItemSeparator = () => {
return (
<View
style={{
height: 1,
width: “100%”,
backgroundColor: “#607D8B”,
}}
/>
);
}
|
4. Create a function named as GetFlatListItem(), This is used to show selected item of FlatList on button click.
1
2
3
4
5
|
GetFlatListItem (item) {
Alert.alert(item);
}
|
5. Create a function named as render_FlatList_header(), This function is used to render View layout for Header in FlatList.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
render_FlatList_header = () => {
var header_View = (
<View style={styles.header_footer_style}>
<Text style={styles.textStyle}> FlatList Header </Text>
</View>
);
return header_View ;
};
|
6. Create another function named as render_FlatList_footer(), This function is used to render Footer at the bottom of FlatList component.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
render_FlatList_footer = () => {
var footer_View = (
<View style={styles.header_footer_style}>
<Text style={styles.textStyle}> FlatList Footer </Text>
</View>
);
return footer_View;
};
|
7. Create the FlatList component inside the Root View in render’s return block.
data : Setting up the FlatList items data source.
ItemSeparatorComponent : Used to render the line between each item.
renderItem : Rendering items in flatlist.
ListHeaderComponent : Calling the render_FlatList_header() function and show header on FlatList.
ListFooterComponent : Calling the render_FlatList_footer() function and showing the footer at bottom of FlatList.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
render() {
return (
<View style={styles.MainContainer}>
<FlatList
data={ this.state.FlatListItems }
ItemSeparatorComponent = {this.FlatListItemSeparator}
renderItem={({item}) => <Text style={styles.FlatList_Item} onPress={this.GetFlatListItem.bind(this, item.key)} > {item.key} </Text>}
ListHeaderComponent={this.render_FlatList_header}
ListFooterComponent={this.render_FlatList_footer}
/>
</View>
);
}
|
8. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
const styles = StyleSheet.create({
MainContainer :{
justifyContent: ‘center’,
flex:1,
paddingTop: (Platform.OS === ‘iOS’) ? 20 : 0
},
FlatList_Item: {
padding: 10,
fontSize: 18,
height: 44,
},
header_footer_style:{
width: ‘100%’,
height: 44,
backgroundColor: ‘#4CAF50’,
alignItems: ‘center’,
justifyContent: ‘center’
},
textStyle:{
textAlign: ‘center’,
color: ‘#fff’,
fontSize: 21
}
});
|
Screenshots in Android device :