I'm trying to design something like the below screenshot in react native. Please note each tile is a Product element fetched from the backend.
But I'm unable to do this using a ListView and its renderRow method which is denying me to use any kind of InfiniteScroll Components.
Currently I'm running a loop with 2 elements at a time and rendering 2 elements inside a scroll view. Below is my Code to explain better.
render() {
var elem = [];
for(var i = 0; i < this.state.products.length; i+=2) {
var prod = this.state.products[i];
var prod2 = this.state.products[i + 1];
elem.push(
<View style={styles.view} key={i} >
<ProductTile onPressAction={this._pdpPage} prod={prod} index={i} />
<ProductTile onPressAction={this._pdpPage} prod={prod2} index={i + 1} />
</View>
);
}
return (
<ScrollView>
{elem}
</ScrollView>
)
}
And then based on the index prop I'm aligning the elements on left or right. My View style looks like below:
view: {
flex: 1,
flexDirection: 'row',
},
Please suggest a better way to do this.
Thanks in advance.
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
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments