62

I've 4 FlatLists with maxHeight set to 200 inside a ScrollView.

<ScrollView>
  <FlatList/>
  <FlatList/>
  <FlatList/>
  <FlatList/>
</ScrollView>

and when I try to scroll a FlatList, it doesn't scroll but the ScrollView scrolls. How do I fix this issue ?

Full Source Code

import { Component, default as React } from 'react';
import { FlatList, ScrollView, Text } from 'react-native';

export  class LabScreen extends Component<{}> {
  render() {
    return (
      <ScrollView>
        {this.renderFlatList('red')}
        {this.renderFlatList('green')}
        {this.renderFlatList('purple')}
        {this.renderFlatList('pink')}
      </ScrollView>
    );
  }

  getRandomData = () => {
    return new Array(100).fill('').map((item, index) => {
      return { title: 'Title ' + (index + 1) };
    });
  };

  renderFlatList(color: string) {
    return (
      <FlatList
        data={this.getRandomData()}
        backgroundColor={color}
        maxHeight={200}
        marginBottom={50}
        keyExtractor={(item, index) => index.toString()}
        renderItem={({ item }) => <Text>{item.title}</Text>}
      />
    );
  }
}

snack.expo link

11 Answers 11

137

We can use the built-in nestedscrollenabled prop for the children FlatList/ScrollView components.

<FlatList nestedScrollEnabled />

This is only required for Android (Nested scrolling is supported by default on iOS).

Sign up to request clarification or add additional context in comments.

7 Comments

thanks it helped, added to parent <ScrollViews prop nestedScrollEnabled={true} /> and to children <FlatList nestedScrollEnabled={true} />
I needed this on iOS for a FlatList inside a ScrollView.
NOT worked for me. <ScrollViews prop nestedScrollEnabled={true} /> <FlatList nestedScrollEnabled={true} /> </ScrollViews>
rnk and @Aniruddha Shevla how is possible that nestedScrollEnabled which is prop of ScrollView ,and used by FlatList
This works perfect for me but still throws the following warning/error: VirtualizedLists should never be nested inside plain ScrollViews with the same orientation because it can break windowing and other functionality
|
40

I was having a very similar issue until I came across an almost complete solution in a very helpful comment on one of the GitHub issues for the react-native project: https://github.com/facebook/react-native/issues/1966#issuecomment-285130701.

The issue is that the parent component is the only one registering the scroll event. The solution is to contextually decide which component should actually be handling that event based on the location of the press.

You'll need to slightly modify your structure to:

<View>
  <ScrollView>
    <View>
      <FlatList />
    </View>
    <View>
      <FlatList />
    </View>
    <View>
      <FlatList />
    </View>
    <View>
      <FlatList />
    </View>
  </ScrollView>
</View>;

The only thing I had to change from the GitHub comment was to use this._myScroll.contentOffset instead of this.refs.myList.scrollProperties.offset.

I've modified your fully working example in a way that allows scrolling of the inner FlatLists.

import { Component, default as React } from "react";
import { View, FlatList, ScrollView, Text } from "react-native";

export default class LabScreen extends Component<{}> {
  constructor(props) {
    super(props);
    this.state = { enableScrollViewScroll: true };
  }

  render() {
    return (
      <View
        onStartShouldSetResponderCapture={() => {
          this.setState({ enableScrollViewScroll: true });
        }}
      >
        <ScrollView
          scrollEnabled={this.state.enableScrollViewScroll}
          ref={(myScroll) => (this._myScroll = myScroll)}
        >
          {this.renderFlatList("red")}
          {this.renderFlatList("green")}
          {this.renderFlatList("purple")}
          {this.renderFlatList("pink")}
        </ScrollView>
      </View>
    );
  }

  getRandomData = () => {
    return new Array(100).fill("").map((item, index) => {
      return { title: "Title " + (index + 1) };
    });
  };

  renderFlatList(color: string) {
    return (
      <View
        onStartShouldSetResponderCapture={() => {
          this.setState({ enableScrollViewScroll: false });
          if (
            this._myScroll.contentOffset === 0 &&
            this.state.enableScrollViewScroll === false
          ) {
            this.setState({ enableScrollViewScroll: true });
          }
        }}
      >
        <FlatList
          data={this.getRandomData()}
          backgroundColor={color}
          maxHeight={200}
          marginBottom={50}
          keyExtractor={(item, index) => index.toString()}
          renderItem={({ item }) => <Text>{item.title}</Text>}
        />
      </View>
    );
  }
}

Hopefully you find this useful!

6 Comments

Not a problem! I had to solve the same issue myself so I figured I may as well share the solution.
What are the values that you use for styling on these elements?
@EricLeFort 2 things: 1. The second answer is much better 2. in you code: " this.setState({ enableScrollViewScroll: false }); if (this._myScroll.contentOffset === 0 && this.state.enableScrollViewScroll === false) {" setState is async function and there is no guarantee that line after you get a false value on this.state.enableScrollViewScroll in a matter of fact almost sure it won't be having the false value
this code work well , but when i switch to another view , it doesn't scroll but when i scroll again it scrolls
Even in 2019, this is the only workaround as far as i know. Tried to use 'nestedscrollenabled' it was not working properly
|
22

This is the simplest answer that requires zero configuration.. and it works like a charm

<ScrollView horizontal={false}>
    <ScrollView horizontal={true}>
        <Flatlist
          ....
          ....
        />
    </ScrollView>
</ScrollView>

1 Comment

Yeaaa, I agree!
20

I fixed my problem with nested FlatList not being able to scroll items on android by simply importing FlatList

import { FlatList } from 'react-native-gesture-handler';

If this would not work, also try to import ScrollView.

import { ScrollView } from 'react-native';
// OR
import { ScrollView } from 'react-native-gesture-handler';

You need to play around with these imports, at least it worked in my case.

1 Comment

I was importing from react-native which caused me invariant violation: withNavigation. Changing to react-native-gesture-handler solved!
10

Try to set the FlatList as nested

nestedScrollEnabled={true}

1 Comment

It has to be done for all ScrollViews ancestors. Also, the "scroll able" feature on FlatList depend on the height and it is set as: style ={{ height: Dimensions.get('window').height / 1.5}}
6

I'm surprised that none of the answers actually worked for me. I went searching for the reason ScrollView and FlatList don't work together and found that there are props on FlatList that can accomodate the header and footer areas above and below my FlatList. If I use this I can make sure the whole screen still scrolls and I don't get the error.

Here is where I found this answer and I've put an example below.

<View>
  <FlatList
    ListHeaderComponent={<View><Text>Top of screen content goes here</Text></View>}
    keyExtractor={(item) => item._id}
    data={[{_id: 1, text: 'one'}, {_id: 2, text: 'two'}]
    renderItem={({item}) => (<View><Text>{item.text}</Text></View>)}
  />
</View>

1 Comment

This worked for my use case. My goal was to have some content above my flatlist but have that content scroll away when trying to view the flatlist. Nesting a ScrollView and FlatList, at least currently, is giving me lots of problems like firing off way too many refreshes and causing instability
1

Using View with a flex:1 instead of ScrollView worked for me.

3 Comments

Did you even read the question? The answer you gave is not possible.
Doesn't matter what people say. this works perfectly for me after hours of following the other answers.
Don't even make sense ;(
0

Make sure you add nestedScrollEnabled to each flatlist / scrollview but most importantly make sure you do not use pos absolute and allow your screen to flex to be able to scroll

Comments

0

Caution

There are some reported cases in which ScrollView imported from 'react-native' is now working properly. For instance this nested scroll issue, I even tried

nestedScrollEnabled

Still it didn't work

On the other hand importing ScrollView from

import { ScrollView } from 'react-native-gesture-handler';

is working fine even without nestedScollEnabled.

Thanks.

Comments

-1

Use map instead of Flatlist, same result and don't break the application

Minha conta
   {
      buttonsProfile.map(button => (
         <ArrowButton 
              key={button.key}
              title={button.title}
              iconName={button.icon} 
              toogle={button.toogle}
              onPress={() => {navigation.navigate(button.route)}}
          />
      ))
    }
                 

4 Comments

Is there some sample code you could share?
see the example below, consider the content as a scroll
This is only viable for a small list, with a large list it can easily run the app out of memory.
The map is not a good option and performs less than a react-native core component.
-3

The better answer is to put a horizontal ScrollView inside of the other ScrollView and then the FlatList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.