简体   繁体   English

你如何让 React Native FlatList 的 ListHeaderComponent 变得有粘性?

[英]How do you make the ListHeaderComponent of a React Native FlatList sticky?

I have a FlatList that is purposefully wider then the screen width.我有一个 FlatList 故意比屏幕宽度更宽。

The list scrolls vertically to view each row and sits in a horizontal ScrollView to access off-screen items.该列表垂直滚动以查看每一行,并位于水平 ScrollView 中以访问屏幕外项目。

The ListHeaderComponent prop is basically an x-axis label I'd like to behave as a "frozen header"; ListHeaderComponent 属性基本上是一个 x 轴标签,我想充当“冻结的标题”; like in a spreadsheet.就像在电子表格中一样。

How do I make the ListHeaderComponent sticky?如何使 ListHeaderComponent 具有粘性?

You need to set prop to Flatlist as stickyHeaderIndices={[0]}您需要将 prop 设置为FlatliststickyHeaderIndices={[0]}

ListHeaderComponent : This prop would set the header view at the top of FlatList . ListHeaderComponent :此道具将在FlatList顶部设置标题视图。

stickyHeaderIndices={[0]} : This prop would set the FlatList 0 index position item as sticky header and as you can see we have already added the header to FlatList so the header is now on 0 index position, so it will by default make the header as sticky. stickyHeaderIndices={[0]} :此道具会将FlatList 0 索引位置项设置为粘性标题,正如您所见,我们已经将标题添加到FlatList ,因此标题现在位于 0 索引位置,因此默认情况下会生成标题为粘性。

<FlatList
  data={ this.state.FlatListItems }
  ItemSeparatorComponent={ this.FlatListItemSeparator}
  renderItem={ ({item}) => (
    <Text
      style={styles.FlatList_Item}
      onPress={this.GetItem.bind(this, item.key)}> {item.key}
      </Text>
  )}
  ListHeaderComponent={this.Render_FlatList_Sticky_header}
  stickyHeaderIndices={[0]}
/>

stickyHeaderIndices={[0]} would solve your problem. stickyHeaderIndices={[0]}可以解决您的问题。

 <FlatList
        data={this.state.data}
        renderItem={this.renderItem}
        keyExtractor={item => item.id}
        stickyHeaderIndices={[0]}
      />

Besides, stickyHeaderIndices can also be an array of the indices we want to stick.此外, stickyHeaderIndices也可以是我们想要粘贴的索引的数组。 You can even set a lot of indices like this: FlatList Sticky Header Example你甚至可以像这样设置很多索引: FlatList Sticky Header Example

 <FlatList
        data={this.state.data}
        renderItem={this.renderItem}
        keyExtractor={item => item.name}
        stickyHeaderIndices={[0, 6, 13]}
      />

When you keep scrolling the FlatList, item0 will be sticky, and then be replaced by item6, item13.当你继续滚动 FlatList 时,item0 会被粘住,然后被 item6、item13 替换。


(source: nativebase.io ) (来源: nativebase.io

You can't find stickyHeaderIndices in the RN FlatList documentation.您在 RN FlatList 文档中找不到stickyHeaderIndices But you can find it in the source code.但是你可以在源代码中找到它。 VirtualizedList supports it. VirtualizedList支持它。

VirtualizedList.js#L964 VirtualizedList.js#L964

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

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