【问题标题】:FlatList not scrolling in React-NativeFlatList 在 React-Native 中不滚动
【发布时间】:2020-12-15 04:48:03
【问题描述】:

我正在尝试使用 FlatList 在 React Native 上构建新闻提要。我希望它类似于Tik Tok的提要,FlatList中的每个项目都占据整个屏幕。

现在它具有我想要的正确布局,但问题是提要不可滚动。

由于某种原因,如果我将孩子设置为 height: '100%',FlatList 将无法滚动。当我将其更改为height: 1000 时,它是可滚动的,但我不想硬编码高度。

如果您能提供任何帮助,我们将不胜感激!

这是提要组件:

const Feed = () => {

  const renderItem = ({ item, index }) => {
    return (
      <FeedItem
        id={item.id.toString()}
        videoUrl={item.videoUrl}
        index={index}
      />
    )
  }

  return (
      <FlatList
        style={styles.container}
        contentContainerStyle={styles.children}
        data={someData}
        renderItem={renderItem}
        keyExtractor={(item, index) => item.id.toString()}
      />
  );
}

// styles
const styles = StyleSheet.create({
  container: {
    display: 'flex',
    flexDirection: 'column',
    height: '100%'
  },
  children: {
    flexGrow: 1,
  }
})

这是提要中的每个项目:

import { Video } from 'expo-av';

const FeedItem = ({ id, videoUrl, index }) => {
  return (
    <Video
      source={{ uri: videoUrl }}
      style={styles.backgroundVideo}
    />
  )
}

const styles = StyleSheet.create({
  backgroundVideo: {
    height: '100%'     // FIXME: problematic
  },
});

【问题讨论】:

  • 你试过Dimensions.get('window').height;吗?
  • 是的,我试过了。但这并没有考虑到底部导航。我希望它占据屏幕而不与导航栏重叠。
  • 嗨,正面临这个确切的问题。你有没有找到解决这个问题的方法?
  • 嘿 Chogo,我最终使用了来自 @react-native-community/viewpager 的 ViewPager。类似于

标签: css reactjs react-native react-native-ios


【解决方案1】:

这可能会有所帮助

const FeedItem = ({ id, videoUrl, index }) => {
  return (
    <View style={{flex:1}}>
      <Video
        source={{ uri: videoUrl }}
        style={styles.videoStyle}
      />
    </View>
  )
}

const styles = StyleSheet.create({
  videoStyle: {
    height: Screen.height,     // Remove this
    width: Screen.width
  },
});

【讨论】:

  • 但如果我这样做,视频根本不可见(零高度)。我该如何解决?
  • 我试过使用屏幕尺寸。我唯一不喜欢的是视频会与底部导航栏重叠。这就是为什么我尝试使用 height: 100% 让它填满剩余的屏幕。
猜你喜欢
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 2017-10-14
相关资源
最近更新 更多