创建来自API数据的FlatList网格

huangapple go评论66阅读模式
英文:

creating flatlist grid from api data

问题

我想在FlatList中创建一个动态网格,但当数据增加时它并不是动态的,我的数据是一个包含图像和其他信息的对象数组。

我正在使用:

columnWrapperStyle={{flexWrap: 'wrap', flex: 1 / 2}}

width: "100%",
height: height / 2.3,


我想创建类似于这样的东西
[![enter image description here][1]][1]

  [1]: https://i.stack.imgur.com/3lcjd.png

如果只有一张图片,网格应该覆盖整个宽度;如果有两张图片,它应该占据一半宽度。

现在主要的问题是,当有三张图片时,第一张的高度应该是完整的,其他的将把高度分成两半,最后一张也是如此。

谢谢。
英文:

I want to create a dynamic grid in the flatlist, but it is not dynamic when data increases, my data is an array of object which has images and other information too.

I am using

columnWrapperStyle={{flexWrap: 'wrap', flex: 1 / 2}}

width: "100%",
height: height / 2.3,

I want to create something like this
创建来自API数据的FlatList网格

if there is one image the grid should cover full width and if two images it should take half of width,

now the main issue I want to do something when there are three images the first height should be full and others will divide the height in half, same for the last one.

thanks.

答案1

得分: 1

import React, { Component } from "react";
import { StyleSheet, View } from "react-native";
import FbGrid from "react-native-fb-image-grid";

class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.inner_container}>
          <FbGrid
            images={[
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
            ]}
          />
        </View>

        <View style={styles.inner_container}>
          <FbGrid
            images={[
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
            ]}
          />
        </View>

        <View style={styles.inner_container}>
          <FbGrid
            images={[
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
            ]}
          />
        </View>

        <View style={styles.inner_container}>
          <FbGrid
            images={[
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
              "https://facebook.github.io/react-native/docs/assets/favicon.png",
            ]}
          />
        </View>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
    backgroundColor: "#eee",
  },
  inner_container: {
    width: "100%",
    height: "20%",
  },
});

export default App;

Live Demo - https://snack.expo.dev/@emmbyiringiro/c182c6

英文:

You should use or get inspired by this package - https://www.npmjs.com/package/react-native-fb-image-grid

import React, { Component } from &quot;react&quot;;
import { StyleSheet, View } from &quot;react-native&quot;;
import FbGrid from &quot;react-native-fb-image-grid&quot;;

class App extends Component {
  render() {
    return (
      &lt;View style={styles.container}&gt;
        &lt;View style={styles.inner_container}&gt;
          &lt;FbGrid
            images={[
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
            ]}
          /&gt;
        &lt;/View&gt;

        &lt;View style={styles.inner_container}&gt;
          &lt;FbGrid
            images={[
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
            ]}
          /&gt;
        &lt;/View&gt;

        &lt;View style={styles.inner_container}&gt;
          &lt;FbGrid
            images={[
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
            ]}
          /&gt;
        &lt;/View&gt;

        &lt;View style={styles.inner_container}&gt;
          &lt;FbGrid
            images={[
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
              &quot;https://facebook.github.io/react-native/docs/assets/favicon.png&quot;,
            ]}
          /&gt;
        &lt;/View&gt;
      &lt;/View&gt;
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: &quot;center&quot;,
    justifyContent: &quot;center&quot;,
    backgroundColor: &quot;#eee&quot;,
  },
  inner_container: {
    width: &quot;100%&quot;,
    height: &quot;20%&quot;,
  },
});

export default App;

Live Demo - https://snack.expo.dev/@emmbyiringiro/c182c6

huangapple
  • 本文由 发表于 2023年2月19日 08:19:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定