Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put a ListView inside a ScrollView using React Native iOS?

I'm building an iOS React Native app. I have some test content and then a list of items below it. If I put both the text content and the listview inside a scrollview the listview does't work properly (only shows some of the items) but if I don't put the listview inside the scrollview, the listview scrolls but the text content stays locked to the top of the screen. How do I get the text content to scroll with the listview?

Here's an example of my issue:
https://rnplay.org/apps/GWoFWg

like image 743
Dev01 Avatar asked Nov 02 '15 15:11

Dev01


People also ask

Can I use FlatList inside ScrollView?

Under the hood, FlatList uses the ScrollView component to render scrollable elements. However, unlike generic ScrollView, FlatList displays data lazily to save memory and processing time. Let's begin with the first method of rendering list data in a React Native app using the map function.

How do you make a list view in React Native?

React Native ListView is a view component which contains the list of items and displays in a vertical scrollable list. The minimum API to create list view is ListView. DataSource. It populates a simple array of data blobs, and instantiate a ListView component with data source and a renderRow callback.

Is FlatList better than ScrollView?

As opposed to the ScrollView, the FlatList renders only those elements that are currently being displayed on the screen (default: 10 items). Thus, it does not have any impact on the performance of the application. So, it is preferable to use the FlatList Component to display a large list of data.


1 Answers

You should render the top content using the ListView.renderHeader prop:

class SampleApp extends React.Component {

  _renderHeader() {
    // your header rendering code here
  }

  _renderRow() {
    // your row rendering code here
  }

  render() {
    return (
      <ListView
        renderRow={this._renderRow}
        renderHeader={this._renderHeader}
        />
    );
  }
}

ListView uses ScrollView internally to provide the scrolling behaviour, so wrapping it in another ScrollView is not necessary.

like image 181
jevakallio Avatar answered Oct 20 '22 21:10

jevakallio