Rect-Native ListView 学习笔记

ListView 学习 类似iOS的 TableView,不过ListView灵活性更强
ListView常用属性 :官方文档

下文学习资料来源:App(Movie Fetcher)

Simulator Screen Shot 2016年8月10日 下午3.05.52.png
    import React, { Component } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      Image, //导入Image组件
      ListView //导入ListView组件
    } from 'react-native';

    //-------------华丽丽模块分界线----------//

    //演示数据
    var MOCKED_MOVIES_DATA = [
      {title: 'Title', year: '2015', posters: {thumbnail: 'https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D360/sign=9f6e24765adf8db1a32e7a623922dddb/0ff41bd5ad6eddc492d491153ddbb6fd52663328.jpg'}},
    ];
    //请求数据 -> Json
    var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';

    class SampleAppMovies extends Component {

      constructor(props) {
        super(props);
        this.state = {
           dataSource: new ListView.DataSource({
            rowHasChanged: (row1, row2) => row1 !== row2,
          }),
          loaded: false,
        };
      }
      componentDidMount() {
        this.fetchData();
      }

      fetchData() {
        fetch(REQUEST_URL)
          .then((response) => response.json())
          .then((responseData) => {
            this.setState({
              dataSource: this.state.dataSource.cloneWithRows(responseData.movies),
              loaded: true,
            });
          })
          .done();
      }

      render() {
        if(!this.state.loaded){
          return this.renderLoadingView();
        }
        return(
          <ListView
            dataSource={this.state.dataSource}
            renderRow={this.renderMovie}
            style={styles.listview}
          />
        );
      }

      renderLoadingView() {
        return (
          <View style={styles.container}>
            <Text>
              Loading movies...
            </Text>
          </View>
        );
      }

      renderMovie(movie) {
        return (
          <View style={styles.container}>
            <Image
              source={{uri: movie.posters.thumbnail}}
              style={styles.thumbnail}
            />
            <View style={styles.rightContainer}>
              <Text style={styles.title}>{movie.title}</Text>
              <Text style={styles.year}>{movie.year}</Text>
            </View>
          </View>
        );
      }
    }

     //-------------风格样式--------------//
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        flexDirection: 'row',
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
      },

      thumbnail: {
        width: 53,
        height: 81,
      },

      rightContainer:{
        flex: 1,
      },

      tilte: {
        fontSize: 20,
        textAlign: 'center',
        marginBottom: 8,
      },
      year: {
        textAlign: 'center',
      },

      listView: {
        paddingTop: 20,
        backgroundColor: '#F5FCFF',
      },

    });

    AppRegistry.registerComponent('SampleAppMovies', () => SampleAppMovies);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,482评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 一楼下棋的老人忽然换了一个 同事美超的发型也变得好看 她还穿上了白色的长裙 也不再和我们同去吃饭 被火烧过的少年已...
    一首诗和小H阅读 128评论 0 0
  • 拥抱的距离 忻君 那天,就在那天 阳光正好 细风正好 掰数着一遍又一遍...
    忻君阅读 336评论 1 4
  • github地址:https://github.com/Ching-Lee/mybatisPro1 1. 需求 根...
    Ching_Lee阅读 316评论 0 0