前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用FlatList构建列表

使用FlatList构建列表

作者头像
mafeifan
发布2018-09-10 12:01:04
1.7K0
发布2018-09-10 12:01:04
举报
文章被收录于专栏:finleyMafinleyMa

接着上一篇 使用react-native-tab-navigator切换页面

当前首页页面内容是空的,只有一个背景色。下面我们来添加些内容。

这里使用 FlatList 来渲染列表。(注:很多教程包含视频中是使用 ListView 构建内容列表的。这个已经被弃用)

步骤如下图非常简单:

  1. 引入FlatList
  2. 写一个 getPageHomeList 方法,可以看到FlatList接收的data属性表示数据源 renderItem表示渲染每条数据的回调方法。这里用Text组件包裹下每条数据。
代码语言:javascript
复制
  getPageHomeList() {
    return (
      <FlatList
        data={[
          {key: 'Devin'},
          {key: 'Jackson'},
          {key: 'James'},
          {key: 'Joel'},
          {key: 'John'},
          {key: 'Jillian'},
          {key: 'Jimmy'},
          {key: 'Julie'},
        ]}
        renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
      />
    );
  }

image.png

  1. 最终把这个方法嵌到View中展示 完整代码如下:
代码语言:javascript
复制
import React from 'react';
import TabNavigator from 'react-native-tab-navigator';
import { StyleSheet, Text, TextInput, View, Image, FlatList } from 'react-native';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {selectedTab: 'home'};
  }

  getPageHomeList() {
    return (
      <FlatList
        data={[
          {key: 'Devin'},
          {key: 'Jackson'},
          {key: 'James'},
          {key: 'Joel'},
          {key: 'John'},
          {key: 'Jillian'},
          {key: 'Jimmy'},
          {key: 'Julie'},
        ]}
        renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
      />
    );
  }

  render() {
    return (
      <View style={styles.container}>
        <TabNavigator>
          <TabNavigator.Item
            selected={this.state.selectedTab === 'home'}
            title="最热"
            renderIcon={() => <Image style={styles.image} source={require('./res/images/ic_polular.png')} />}
            renderSelectedIcon={() => <Image style={styles.image} source={require('./res/images/ic_polular.png')} />}
            badgeText="1"
            onPress={() => this.setState({ selectedTab: 'home' })}>
            <View style={styles.page1}>
              {this.getPageHomeList()}
            </View>
          </TabNavigator.Item>
          <TabNavigator.Item
            selected={this.state.selectedTab === 'profile'}
            title="趋势"
            renderIcon={() => <Image style={styles.image} source={require('./res/images/ic_trending.png')} />}
            renderSelectedIcon={() => <Image style={styles.image} source={require('./res/images/ic_trending.png')} />}
            onPress={() => this.setState({ selectedTab: 'profile' })}>
            <View style={styles.page2}></View>
          </TabNavigator.Item>
        </TabNavigator>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
  page1: {
    flex: 1,
    backgroundColor: 'red'
  },
  page2: {
    flex: 1,
    backgroundColor: 'yellow'
  },
  item: {
    padding: 10,
    fontSize: 18,
    height: 44,
  },
  image: {
    height: 22,
    width: 22
  }
});

文章里还介绍了比FlatList稍微复杂些的 SectionList 组件。当需要对item进行分组,支持设置每个分组的header,footer。

这个非常适合用来做通讯录,城市地址

demo.gif

参考文档:

参考:

http://facebook.github.io/react-native/docs/using-a-listview.html

http://blog.csdn.net/mengks1987/article/details/70229918

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.11.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档