简体   繁体   English

如何在 React Native 平面列表中滚动期间隐藏 ListHeaderComponent

[英]How do I hide the ListHeaderComponent during scroll in a React Native flatlist

I am trying to render a flatlist with a header.我正在尝试使用 header 呈现平面列表。 The header needs to be stuck at the top of the flatlist but needs to hide when the user scrolls down. header 需要卡在平面列表的顶部,但需要在用户向下滚动时隐藏。 When the user starts scrolling up again the header needs to be displayed.当用户再次开始向上滚动时,需要显示 header。 I'm able to get the header to stick using stickyHeaderIndices.我能够让 header 坚持使用stickyHeaderIndices。 However, StickyHeaderHiddenOnScroll does not seem to work at all.但是,StickyHeaderHiddenOnScroll 似乎根本不起作用。 From what I understand, StickyHeaderComponent is used in a scrollView instead of ListHeaderComponent but that doesn't seem to render any header in the case of a flatlist.据我了解,StickyHeaderComponent 用于 scrollView 而不是 ListHeaderComponent 但在平面列表的情况下似乎不会呈现任何 header 。

<FlatList               
                        data={selectedStream} 
                        keyExtractor={keyExtractor}    
                        extraData = {selectedGroup}      
                        renderItem={renderItem}      
                        stickyHeaderIndices={[0]}
                        stickyHeaderHiddenOnScroll={true}
                        ListHeaderComponent={()=> <ComponentThatNeedsToBeStuckToTop/>}
                        />

You need to use the onScroll function to check if the Flatlist was scrolled or not您需要使用 onScroll function 来检查 Flatlist 是否滚动

  const onScroll = useCallback(({ nativeEvent }) => {
    if (nativeEvent.contentOffset.y > 40) setHeaderTitle('The header title');
    if (nativeEvent.contentOffset.y <= 40) setHeaderTitle(' ');
  }, []);

In the Flatlist:在平面列表中:

  <FlatList
   data={data}
      keyExtractor={keyExtractor}
      renderItem={renderItem}
      scrollEventThrottle={16}
      onScroll={onScroll}
      ListHeaderComponent={renderHeader}
      ListFooterComponent={renderFooter}
    />

Checkout this video tutorial you will get what you want to do -看看这个视频教程,你会得到你想做的 -

https://youtu.be/nayqNApYp-I https://youtu.be/nayqNApYp-I

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

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