【问题标题】:How to fix flatlist is not shown react native未显示如何修复 flatlist 反应原生
【发布时间】:2020-05-10 15:26:23
【问题描述】:

我在抽屉导航器中使用列表,它包含两个列表,一个是静态的,另一个是动态的(从 api 获得的项目),但是我需要使用平面列表而不是列表(用于使用 onEndReached 实现无限滚动) ,当我使用它时,列表中的任何一个都会变为空白。

包含两个列表(静态和动态)的列表:

  return (
      <View style={{ flex:1 }}>
          <View style={{ height:80,alignItems: 'center', justifyContent: 'center' }}>
           <Image source={IMAGE.ICON_MENU} style={{ height:60,width:160 }}/>
          </View>
          <ScrollView>
          <View style={{flexDirection: 'column',flex:1,  justifyContent: 'center',alignItems: 'stretch' }}>
          <List style={{marginBottom: 30}} >
            <ListItem  style={{ marginLeft: 0, marginBottom: 6,backgroundColor: 'red' }}  onPress={() => this.props.navigation.navigate('Politik')}>
             <Icon style={{marginLeft: 20}} name={'sign-in'} size={20} color={"white"} />
             <Text style={{ marginLeft: 10,color: 'white' }} >Log in</Text>
            </ListItem>
            <ListItem  style={{ marginLeft: 0,backgroundColor: 'red' }}  onPress={() => this.props.navigation.navigate('Politik')}>
             <Icon style={{marginLeft: 20}} name={'home'} size={20} color={"white"} />
             <Text style={{marginLeft: 10,  color: 'white' }}>Top-News</Text>
            </ListItem>
          </List>
         </View>
            {DynamicList}
         </ScrollView>
       </View>
    );

动态列表:

const DynamicList=this.state.data?
(
  <View style={{}}>
  <List style={{flex:1 }}
  dataArray={this.state.data}
  keyExtractor={(item,index) => index.toString()}
renderItem={({item,index})=>{
return (
           <ListItem  style={{  marginLeft: 20,marginRight: 20}}  onPress={() => this.props.navigation.navigate('Feed',{urll:`https://nux3.tageblatt.lu/${item.feed}`}) &&this.props.navigation.closeDrawer()}>
           <Text style={{ marginLeft: 0 }}>{item.label}</Text>
           </ListItem >
        )
  }} />
    </View>
):null;

我尝试删除 ScrollView 但没有任何变化。

【问题讨论】:

    标签: react-native


    【解决方案1】:

    您应该将所有内容包装在FlatList 而不是ScrollView

    
    const DrawerContent = () => {
      const myDynamicItems = [
        // ...
      ];
      const renderItem = ({ title, iconId, onPress }) => {
        return (
          <ListItem
            style={{ marginLeft: 0, marginBottom: 6, backgroundColor: 'red' }}
            onPress={onPress}
          >
            <Icon style={{ marginLeft: 20 }} name={iconId} size={20} color={'white'} />
            <Text style={{ marginLeft: 10, color: 'white' }}>{title}</Text>
          </ListItem>
        );
      };
      const data = [
        {
          title: 'Log In',
          onPress: () => this.props.navigation.navigate('Politik'),
          iconId: 'log-in',
        },
        // other static items
        // other dynamic items
        ...myDynamicItems,
      ];
      return (
        <View style={{ flex: 1 }}>
          <View style={{ height: 80, alignItems: 'center', justifyContent: 'center' }}>
            <Image source={IMAGE.ICON_MENU} style={{ height: 60, width: 160 }} />
          </View>
          <FlatList
            renderItem={renderItem}
            data={data}
            // other props
          />
        </View>
      );
    };
    

    【讨论】:

    • 您好,谢谢您的回答,我之前尝试删除 ScrollView,但 flatlist 仍然是空白的。
    • 你的意思是把静态数据和动态数据合并成一个数组吗?
    • 我尝试删除滚动视图和动态数据,仍然没有显示。
    • Do you mean combine the static and dynamic data into one array – 是的,没错! I tried to remove both the scrollview and the dynamic data, still not shown. – 尝试在FlatList 上设置style={{ flex: 1 }}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2018-04-17
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多