如何在React Native的FlatList中水平显示图像?

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

How to display images horizontally in flatlist react native?

问题

我正在尝试在使用FlatList时水平显示动态图片,而不是垂直显示。我该如何做呢?我尝试将动态图片包装到一个视图中,然后为其设置flex-direction: row,还尝试给我的FlatList添加horizontal={true},但所有这些方法都不起作用。有人可以帮忙吗?

我使用我的代码创建了一个小应用,你可以从这里访问代码:https://codesandbox.io/s/runtime-leaf-jywqqr?file=/src/App.js

英文:

I am trying to display feed images horizontally instead of vertically in using FlatList How can I do that? I tried wrapping feed images into a view and then giving it a flex-direction row I also tried to give horizontal={true} to my FlatList but all these methods are not working Can anyone help please to do that?

I created a small app using my code you can access the code from here https://codesandbox.io/s/runtime-leaf-jywqqr?file=/src/App.js

答案1

得分: 1

horizontal 部分可以正常工作,以下是一个简单的示例:

const Photo = ({download_url}) => (
  <Image style={{width: 320, height: 180}} source={{uri: download_url}} />
);

const App = () => {
  const [photos, setPhotos] = useState([]);
  useEffect(() => {
    const getPhotos = async () => {
        const response = await fetch("https://picsum.photos/v2/list");
        if (response.ok) {
          setPhotos(await response.json());
        }
    };
    getPhotos();
  }, []);
  return (
    <FlatList
      data={photos}
      horizontal
      renderItem={({item}) => <Photo download_url={item.download_url} />}
      keyExtractor={item => item.id}
    />
  );
};
英文:

horizontal works just fine, here is the simple example:

const Photo = ({download_url}) =&gt; (
  &lt;Image style={{width: 320, height: 180}} source={{uri: download_url}} /&gt;
);

const App = () =&gt; {
  const [photos, setPhotos] = useState([])
  useEffect(() =&gt; {
    const getPhotos = async () =&gt; {
        const response = await fetch(&quot;https://picsum.photos/v2/list&quot;)
        if (response.ok) {
          setPhotos(await response.json())
        }
    }
    getPhotos()
  }, [])
  return (
    &lt;FlatList
      data={photos}
      horizontal
      renderItem={({item}) =&gt; &lt;Photo download_url={item.download_url} /&gt;}
      keyExtractor={item =&gt; item.id}
    /&gt;
  );
};

答案2

得分: 0

你需要为项目设置宽度和高度。

英文:

I think you need set width, height for item

huangapple
  • 本文由 发表于 2023年2月24日 00:02:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547320.html
匿名

发表评论

匿名网友

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

确定