简体   繁体   English

如何滚动呈现两个 flatList 的整个屏幕?

[英]How do I scroll an entire screen that renders two flatLists?

I have two flatLists nested inside of a scrollView so I am able to scroll my entire page.我在 scrollView 中嵌套了两个 flatList,因此我可以滚动整个页面。 However, I know that you are not supposed to nest flatLists in scrollViews for multiple reasons.但是,我知道出于多种原因,您不应该在 scrollViews 中嵌套 flatLists。

How can I render two flatLists while still being able to scroll throughout the entire page?如何在呈现两个 flatList 的同时仍然能够滚动整个页面? The GIF at the bottom of the post is the desired behavior I want.帖子底部的 GIF 是我想要的行为。

I created a snack post here as well as provided some example code below.我在这里创建了一个小吃帖子,并在下面提供了一些示例代码。

export default function App() {

  return (
    <View style={{ alignItems: 'center', marginTop: 100, flex: 1}}>
        <FlatListB/>
        <FlatListA/>
    </View>
  );
}
  return (
    <FlatList
      data={newData}
      renderItem={renderItem}
      onEndReached={fetchMoreBars}
      onEndReachedThreshold={0.1}
    />
  );
  return (
    <FlatList
      data={newData}
      renderItem={renderItem}
      onEndReached={fetchMoreBars}
      onEndReachedThreshold={0.1}
      horizontal={true}
    />
  );

https://giphy.com/gifs/7V07FvYyn8ZG3nwVVU - This GIF was created by nesting FlatListB and FlatListA in a ScrollView https://giphy.com/gifs/7V07FvYyn8ZG3nwVVU - 这个 GIF 是通过在 ScrollView 中嵌套 FlatListB 和 FlatListA 创建的

If you would like to prevent nested flatLists in scrollView, you may use ListHeaderComponent props in FlatList.如果你想防止在 scrollView 中嵌套 flatList,你可以在 FlatList 中使用ListHeaderComponent属性。

FlatListA.js : FlatListA.js

 <FlatList
   data={newData}
   renderItem={renderItem}
   onEndReached={fetchMoreBars}
   onEndReachedThreshold={0.1}
   ListHeaderComponent={<FlatListB />}
 />

App.js应用程序.js

export default function App() {
  return (
    <FlatListA/>
  );
}

This should work if 2 flatlists are not scrolling in same direction (Vertical / Horizontal).如果 2 个平面列表不在同一方向(垂直/水平)滚动,这应该有效。

try this code试试这个代码

 const Data = [1,2,3,4,1,1,1,1,1,1,1,]
const Horizontal = () => {
    const Data = [1,2,3,4,1,1,1]
    return <FlatList
        horizontal
        data={Data} renderItem={() => {
        return <View style={{height:100,width:100,backgroundColor:'pink',margin:2}}>
        </View>
    }}/>
}
const ListHeaderComponent = () => {
    return  <Horizontal/>
}
<View >
    <FlatList
        ListHeaderComponent={ListHeaderComponent}
        data={Data} renderItem={() => {
        return <View style={{height:100,width:100,backgroundColor:'white',margin:2}}>
        </View>
    }}/>
</View>

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

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