【问题标题】:Is it possible for section items to be in a row?部分项目是否可以连续排列?
【发布时间】:2019-07-24 01:35:01
【问题描述】:

我在 React Native 中使用 SectionList 来显示从 URL 获取的一些 JSON 数据。

<SectionList
            sections = {this.state.dataSource}
            renderSectionHeader = {({section}) => <Text style={styles.TopicNameContainer}>{section.title}</Text>}
            renderItem = {({item}) => 
                <ScrollView style={{height: 50}} horizontal={true}>
                    <ImageBackground style={{width: '100%', height: '100%', justifyContent: 'flex-end'}} source={{ uri: 'http://localhost:1337' + item.ChapterImage.url }}>
                        <Text style={[styles.ChapterNameContainer, {backgroundColor: 'rgba(255, 255, 255, 0.5)', }]}>{item.ChapterName}</Text>
                    </ImageBackground>
                </ScrollView>}
            keyExtractor = {({id}, index) => id.toString()}
/>

有了上面的代码,就这样出现了;

章节标题 1

  • 第 1.1 项
  • 第 1.2 节

章节标题 2

  • 第 2.1 节

但是,我希望它如下所示;

章节标题 1

第 1.1 节 |第 1.2 节

章节标题 2

第 2.1 节

上面的部分项目有水平滚动。

我已经尝试将 ScrollList 设置为 horizontal={true},但这样做会使整个内容变为水平滚动,但是,我不希望 Section Header 水平滚动。

更新:经过更多研究,人们建议在 SectionList 中使用 FlatList。我试过了,但它仍然垂直显示。

renderItem={({ item }) => (
  <FlatList
    style={{height: 50}} 
    horizontal={true}
    data={Array(item)}
    renderItem={({ item }) => 
    <View>
      <ImageBackground style={{width: '100%', height: '100%', justifyContent: 'flex-end'}} source={{ uri: 'http://localhost:1337' + item.ChapterImage.url }}>
         <Text style={[styles.ChapterNameContainer, {backgroundColor: 'rgba(255, 255, 255, 0.5)', }]}>{item.ChapterName}</Text>
      </ImageBackground>
    </View>
    }
    keyExtractor = {({id}, index) => id.toString()}
/>)}

【问题讨论】:

    标签: react-native mobile react-native-scrollview react-native-sectionlist


    【解决方案1】:

    您应该试试flexdirectionView

                renderItem = {({item}) => 
                    <View style={{ flexDirection: "row" }}>
                    <ScrollView style={{height: 50, width: '50%' }} >
                        <ImageBackground style={{width: '100%', height: '100%', justifyContent: 'flex-end'}} source={{ uri: 'http://localhost:1337' + item.ChapterImage.url }}>
                            <Text style={[styles.ChapterNameContainer, {backgroundColor: 'rgba(255, 255, 255, 0.5)', }]}>{item.ChapterName}</Text>
                        </ImageBackground>
                    </ScrollView> 
                    <View>}
    

    【讨论】:

    • 在发布这个问题之前,我确实尝试过。它不起作用。我认为问题在于 renderItem 将分别渲染这些项目。意思是,每个项目都有自己的 View 或 ScrollView,因此即使它已经是 ScrollView horizontal = {true} 或 View flexDirection: 'row',项目也不在一行中的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 2013-06-09
    • 2021-09-27
    • 1970-01-01
    • 2013-06-27
    相关资源
    最近更新 更多