I'm want to create the simplest two column grid ListView
item | item
item | item
I saw this post about creating the grid and that worked - ListView grid in React Native
I couldn't understand how to make my grid responsive? How can I set the size of the item to 50%?
I know it is a new language but I wish it could have some more detailed guides / documents.
A grid layout can be defined as a layout in which components are arranged in form of rows and columns thus providing an easy way of allocating components on user interface without use of positioning and floating values, in order to use react native grid layout we need to include dependency of react native grid.
A responsive grid allows a layout to change dynamically based on the size of the screen. This also guarantees consistent layouts across Adobe's products.
Building a Responsive Grid-View First ensure that all HTML elements have the box-sizing property set to border-box . This makes sure that the padding and border are included in the total width and height of the elements. Read more about the box-sizing property in our CSS Box Sizing chapter.
I just had to do that and I could solve it with simple react Views and the flexWrap rule.
Parent view
{ flex: 1, flexDirection: 'row', flexWrap: 'wrap'}
Child View (Items)
{ width: '50%' }
Hope that this might help someone!
It's very simple, Use flex box, In your render method return view contains rows, and use for loop for putting data.
const rowData=[];
render() {
return <View style={styles.container}>{rowData}</View>;
}
for (let objectData of newArray) {
rowData.push(this.renderRowView(objectData));
}
renderRowView(objectData) {
return(
<View style={styles.mainContainer}>
<View style={styles.leftView}></View>
<View style={styles.rightView}></View>
</View>
);
}
const styles = StyleSheet.create({
mainContainer: {
flex:1,
},
leftView: {
flex:1,
},
rightView: {
flex:1,
}
});
I have not run this code but hope it works for you!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With