Render two items (images) side by side in a list view in react native
A good way we have done this in production in the past, and it has worked out well, is to get the width of the container and set the width of the cards to 50% of the width, then you can just push all of the single elements into the listview. Also, be sure to set up a flexWrap
of wrap
.
This will work across all device sizes, and requires not additional modules or libraries.
Check out the sample code below and example here:
https://rnplay.org/apps/t_6-Ag
/* Get width of window */
const width = Dimensions.get('window').width
/* ListView */
<ListView
contentContainerStyle={styles.listView}
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
/>
/* Row */
renderRow () {
return <View style={styles.card}>
<Text>{rowData.name} {rowData.price}</Text>
</View>
/* Styles */
listView: {
flexDirection: 'row',
flexWrap: 'wrap'
},
card: {
backgroundColor: 'red',
width: (width / 2) - 15,
height: 300,
marginLeft: 10,
marginTop: 10
}
React Native has a good example of this in their CameraRollView.js
example. They use a library called groupByEveryN
to allow you to set how many items to render per row.
Notice the change to how you initiate the ListView.DataSource
...
var ds = new ListView.DataSource({rowHasChanged: this._rowHasChanged});
this.state.dataSource = ds.cloneWithRows(
groupByEveryN(this.state.assets, this.props.imagesPerRow)
);
Your renderRow
function just needs to expect an array of items...
// rowData is an array of images
_renderRow: function(rowData: Array<Image>, sectionID: string, rowID: string) {
var images = rowData.map((image) => {
if (image === null) {
return null;
}
return this.props.renderImage(image);
});
return (
<View style={styles.row}>
{images}
</View>
);
}
Full example file here: https://github.com/facebook/react-native/blob/a564af853f108e071e941ac08be4cde864f5bfae/Examples/UIExplorer/js/CameraRollView.js.