简体   繁体   English

如何在 React Native 中创建 2 行 3 列可见的水平滚动 Flatlist?

[英]How to create Horizontal Scrolling Flatlist with 2 rows and 3 columns visible in React Native?

I want to create a Horizontal Scrolling Flatlist with 2 rows and 3 columns visible.我想创建一个具有 2 行和 3 列可见的水平滚动平面列表。 Data will be dynamic from api. I have managed to create vertical scrolling Flatlist which looks like this:数据将从 api 开始动态变化。我设法创建了垂直滚动的 Flatlist,如下所示: 垂直滚动平面列表 . . There is numOfColumns to specify count of columns but there is nothing for rows.numOfColumns指定列数,但行没有任何内容。 When I tried to set horizontal={true} , layout became weird:当我尝试设置horizontal={true}时,布局变得很奇怪: 水平滚动平面列表 Also it throws error that numOfColumns can not be applied with Horizontal Scrolling Flatlist.它还会抛出numOfColumns不能应用于水平滚动 Flatlist 的错误。 Please refer attached screenshot for Desired output:请参考所需 output 的附加屏幕截图: 期望的输出 . .

My Code for Vertical Scrolling Flatlist:我的垂直滚动平面列表代码:

<FlatList
        ref={(ref) => { this.flatListRef = ref; }}
        style={{ width: '90%', marginTop: 10, marginLeft: 10, backgroundColor: 'transparent', borderRadius: 5, alignSelf: 'center' }}
        bounces={false}
        // horizontal={true}
        numColumns={3}
        data={this.state.categoryData}
        renderItem={({ item: data, index }) => {
        return (
          <ServiceView viewWidth={'100%'} viewHeight={'100%'} viewPaddingLeft={1} viewPaddingRight={10} viewPaddingTop={1} viewPaddingBottom={12} serviceName={(globalUserType == 'provider') ? data.services.name : data.name} serviceIconUrl={(globalUserType == 'provider') ? data.services.image : data.image} jobCount={(globalUserType == 'provider') ? '' : ''} showDot={(globalUserType == 'provider') ? false : false} 
           serviceAction={
           () => this.navigateTo('category', data)

         }/>

What changes should i make to get desired output.我应该做哪些更改才能获得所需的 output。

class App extends Component {
  renderItemForFirstList = ({item, index}) => {
    <FlatList
      data={item}
      renderItem={this.renderItemForSecondList}
    />
  }
  render() {
    return (
      <View>
        <FlatList
          data={this.state.categoryData}
          renderItem={this.renderItemForFirstList}
        />
      </View>
    );
  }
}

You can achieve this by adding Horizontal Flatlist in Vertical Flatlist Items like this:您可以通过在垂直平面列表项中添加水平平面列表来实现这一点,如下所示:

Vertical FlatList
<FlatList  
   data={[  
   {imageKey: 'image1',imageKey: 'image2',imageKey: 'image3'},{imageKey: 'image4',imageKey: 'image5',imageKey: 'image6'},{imageKey: 'image7',imageKey: 'image8',imageKey: 'image9'} ]}  
    renderItem={({item}) =>  
       <FlatList  
         horizontal={true}
         showsHorizontalScrollIndicator={false}
         data={item}  
         renderItem={({item}) =>  
          <Text>{item.key}</Text>}  
       /> 
   />  

Same issue happened, when trying to render desired output of this question from this type of json array data尝试从这种类型的 json 数组数据呈现此问题的所需输出时,发生了同样的问题

[{"id": 1, "name": "John", "image": "/api/12.jpg"}, {"id": 2, "name": "James", "image": "/api/14.jpg"}, {"id": 3, "name": "David", "image": "/api/172.jpg"} ...]

i tried many way to render in two rows format, but could not.我尝试了很多方法以两行格式呈现,但不能。 So, i changed the json in these format所以,我以这些格式更改了 json

    [
      [
         {"id": 1, "name": "John", "image": "/api/12.jpg"}, 
         {"id": 2, "name": "James", "image": "/api/14.jpg"}, 
         {"id": 3, "name": "David", "image": "/api/172.jpg"} 
           ...
      ],
      [
         {"id": 9, "name": "Kim", "image": "/api/12hj.jpg"}, 
         {"id": 10, "name": "Jim", "image": "/api/14ff.jpg"}, 
         {"id": 11, "name": "Rose", "image": "/api/1723.jpg"} 
         ...
      ]
]

There will two way we can get this type of json format有两种方法可以获得这种类型的 json 格式

  1. From backend api从后端api
  2. By spliting jsonarray into chunk arrays into one array using Javascript code in componentDidmount or in function.通过在 componentDidmount 或在函数中使用 Javascript 代码将 jsonarray 拆分为块数组,成为一个数组。

then you can use this code to perform the desired output of this question那么您可以使用此代码来执行此问题的所需输出

<ScrollView
      
        horizontal={true}
        showsHorizontalScrollIndicator={false}
      
      >
        //two row formated json array
        {this.state.checkData.map((dd, index) => 
          
            <FlatList
           
            contentContainerStyle={{alignSelf: 'flex-start'}}
            numColumns={3} //<---------- here we can change the number of rows
            data={dd}
            renderItem={({ item: rowData }) => {
           
              return (
                <TouchableOpacity style={{ padding: 6}} onPress={() => this.props.navigation.navigate('RDetails', { id: rowData.id })}>

                  <View style={{flex: 1, height:110, width: 96}}>

                  
                  <View style={{ flex: 1 }}>
                    <Image resizeMode='stretch' style={{ borderRadius: 5, width: 80, height: 70, margin: 2 }} source={{ uri: rowData.image }} />

                  </View>
                  
              </View>

                </TouchableOpacity>
              );

            }
            }
            keyExtractor={(item, index) => index.toString()}
          />
        )}
      </ScrollView>

There another way using _.chunk from Lodash.还有另一种使用来自Lodash的 _.chunk 的方法。

Example:例子:

{_.chunk(data, 2).map((item, index) => (
  <View key={index} style={{flexDirection: "column"}>
  {item.map((subItem, subIndex) => (

  <Text key={subIndex}>{subItem}</Text>

  ))}
  </View>
))}

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

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