Render two items (images) side by side in a list view in react native

Karan Gujral

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.

Product tiles

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.

Nader Dabit

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Aligning items of a list in the right side

From Dev

Plot two images side by side with skimage

From Dev

Getting two images to show side by side, with some complications

From Dev

Centering two images side-by-side with captions

From Dev

Render React JS server side using .net

From Dev

Align two images side by side

From Dev

How to get rid of the dots on the side of list items?

From Dev

React-router unable to render on server side

From Dev

How to render two pd.DataFrames in jupyter notebook side by side?

From Dev

React Native View Render

From Dev

Plotting two images side by side in python

From Dev

Two images side by side and responsive

From Dev

Position images side by side

From Dev

React Native list view does not render whole list

From Dev

List of items from the inverside side of a ManyToOne relationship

From Dev

SwiftUI - place two list side-by-side

From Dev

Two divs side by side

From Dev

Two buttons side by side

From Dev

How to view two PDF files side-by-side in Foxit?

From Dev

Adding images to a table view on left side

From Dev

How do I put two or more images side by side in HTML?

From Dev

Placing to List Items side by side in android ListView

From Dev

list items along side images in navigation bar

From Dev

React Native list view does not render whole list

From Dev

how to arrange 2 images side by side in mobile view?

From Dev

React Context undefined in Server Side Render

From Dev

How to position two images side by side in svg

From Dev

Flex: Align two items side by side without max width

From Dev

How to render items in two columns in react native?