gpt4 book ai didi

react-native - sectionList REACT NATIVE 中的自定义部分样式

转载 作者:行者123 更新时间:2023-12-04 04:21:47 30 4
gpt4 key购买 nike

你知道如何在 sectionList 组件 React-native 中制作水平部分(特定)吗?我想让第二部分水平,我尝试使用 flex:1 和 flexDirection: 'row' 修改 renderItem 中的项目样式,但不起作用。任何人都知道如何在截面上设置自定义样式或制作水平截面? (红圈内)

        <View>
<SectionList
renderItem={({item, index, section}) => <CellMainNews isFirst={index===0 ? true: false} data={ item } onPress = {item.onPress } />}
renderSectionHeader={({section: {title}}) => (
<Text style={{fontWeight: 'bold'}}>{title}</Text>
)}
sections={[
{title: 'Top post', data: this.props.featured.top, renderItem: overrideRenderItem },
// this section
{title: 'Featured posts', data: this.props.featured.secundary, renderItem: overrideRenderItemTwo },
{title: 'Stories', data: this.props.stories},
]}
keyExtractor={(item, index) => item + index}
/>

{this.props.loading &&
<View>
<ActivityIndicator size={100} color="red" animating={this.props.loading} />
</View>
}
</View>

enter image description here

问候。

最佳答案

SectionList 开始有点难了不会在其部分上放置容器 View ,但您可以通过传递包含所述元素上所有项目的单个元素数组来实现它。

您可以使用您偏好的组件以您想要的方式呈现该部分上的所有项目。

我建议使用 FlatList

<View>
<SectionList
renderItem={({item, index, section}) => <CellMainNews isFirst={index===0 ? true: false} data={ item } onPress = {item.onPress } />}
renderSectionHeader={({section: {title}}) => (
<Text style={{fontWeight: 'bold'}}>{title}</Text>
)}
sections={[
{title: 'Top post', data: this.props.featured.top, renderItem: overrideRenderItem },
{title: 'Featured posts', data: [this.props.featured.secundary], renderItem: overrideRenderItemTwo }, // Passing here the single element array
{title: 'Stories', data: this.props.stories},
]}
keyExtractor={(item, index) => String(index)}
/>

{this.props.loading &&
<View>
<ActivityIndicator size={100} color="red" animating={this.props.loading} />
</View>
}
</View>

还有你的 overrideRenderItemTwo
const overrideRenderItemTwo = ({ item, index, section: { title, data } }) => {
return (
<FlatList
showsHorizontalScrollIndicator={false}
pagingEnabled={true}
horizontal={true}
data={item}
keyExtractor={(item, index) => String(index)}
renderItem={(
({item}) => (<CellMainNews isSecundary={true} isFirst={index===0 ? true: false} data={ item } onPress = {item.onPress } />)
)}
/>
);
}

这样做的好处是你可以为特定部分的容器 View 使用你想要的样式

关于react-native - sectionList REACT NATIVE 中的自定义部分样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50500546/

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