gpt4 book ai didi

react-native - SectionList React Native 上的粘性 header

转载 作者:行者123 更新时间:2023-12-05 09:32:23 27 4
gpt4 key购买 nike

我需要创建一个屏幕目录(类别和产品)。我正在使用 React Native 的 SectionList 来实现这一点。

当您滚动产品列表时,我需要使该类别组件保持在顶部。是否有任何图书馆可以帮助我处理此目录屏幕? Please look at the image here..

import React from "react";
import { View, StyleSheet, SectionList } from "react-native";

import Text from "../Text";

const DATA = [
{
title: "Main dishes",
data: ["Pizza", "Burger", "Risotto"],
},
{
title: "Sides",
data: ["French Fries", "Onion Rings", "Fried Shrimps"],
},
{
title: "Drinks",
data: ["Water", "Coke", "Beer"],
},
{
title: "Desserts",
data: ["Cheese Cake", "Ice Cream"],
},
];

const TabCategories = () => (
<View>
<Text>Horizontal list of categories</Text>
</View>
);

const Item = ({ title }) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);

const TestSectionList = (props) => {
return (
<View style={styles.container}>
<Text style={styles.SRC}>Some React Component</Text>
<SectionList
sections={DATA}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => <Item title={item} />}
renderSectionHeader={({ section: { title } }) => (
<Text style={styles.header}>{title}</Text>
)}
StickyHeaderComponent={TabCategories}
/>
</View>
);
};

const styles = StyleSheet.create({
container: {},
SRC: {
fontWeight: "bold",
borderWidth: 1,
borderColor: "#fff",
padding: 10,
},
item: {
padding: 30,
},
header: {
fontWeight: "bold",
fontSize: 20,
},
});

export default TestSectionList;

最佳答案

stickySectionHeadersEnabled使节标题粘在屏幕顶部,直到下一个将其向上推

ListHeaderComponent在列表的最开头呈现

renderSectionHeader呈现在每个SECTION的顶部

我认为应该这样做:

<SectionList
sections={DATA}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => <Item title={item} />}
ListHeaderComponent={({ section: { title } }) => (
<Text style={styles.header}>{title}</Text>
)}
renderSectionHeader={TabCategories}
stickySectionHeadersEnabled
/>

关于react-native - SectionList React Native 上的粘性 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68049680/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com