简体   繁体   English

在React Native中的ScrollView中滚动FlatList

[英]scroll FlatList in a ScrollView in React Native

I have a FlatList inside a View , and the View is at the end of a ScrollView , I want to scroll the FlatList while the whole scroll at the end of the page. 我在View内有一个FlatList ,而ViewScrollView的末尾,我想滚动FlatList而整个滚动都在页面末尾。 But I can't do that. 但是我做不到。 So what should I do to let my FlatList have the ability to scroll while inside a ScrollView ? 那么,如何使FlatList能够在ScrollView呢? The body is like this: 身体是这样的:

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

You can solve this issue by adding some code in to View which wraps the ScrollView and the another View which wraps the FlatList 您可以通过添加一些代码解决这个问题, View包装了ScrollView和另一View包装了FlatList

So first of all define a user define variable in state inside the constructor. 因此,首先在构造函数内部以state定义用户定义变量。

this.state={
    .....
    enableScrollViewScroll : false
    ....
 }

Then add following code as a props to the View which wrap the ScrollView 然后将以下代码作为props添加到包装ScrollViewView

onStartShouldSetResponderCapture={() => {
  this.setState({ enableScrollViewScroll: true });
}}

Add scrollEnabled={this.state.enableScrollViewScroll} prop to ScrollView scrollEnabled={this.state.enableScrollViewScroll}添加到ScrollView

And Finally add following to the View which wraps the FlatList( immediate parent ) 最后,在包装FlatList(直接父对象)的View中添加以下内容

onStartShouldSetResponderCapture={() => {
   this.setState({ enableScrollViewScroll: false });

  if (this.refs.myList.scrollProperties.offset === 0 && 
     this.state.enableScrollViewScroll === false) {
     this.setState({ enableScrollViewScroll: true });
 }
}}

Here myList is the referance of FlatList 这里myListFlatList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM