电影列表

import React, { Component } from 'react'
import { View, Image, Text, ActivityIndicator, FlatList, StyleSheet, TouchableHighlight } from 'react-native' const styles = StyleSheet.create({
movieTitle: {
fontWeight: 'bold'
}
}) // 导入路由的组件
import { Actions } from 'react-native-router-flux' export default class MovieList extends Component {
constructor(props) {
super(props)
this.state = {
movies: [], // 电影列表
nowPage: 1, // 当前的页码
totalPage: 0, // 总页数
pageSize: 15, // 每页显示的记录条数
isloading: true // 是否正在加载数据
}
} componentWillMount() {
this.getMoviesByPage()
} render() {
return <View>
{this.renderList()}
</View>
} // 根据页码获取电影列表
getMoviesByPage = () => {
const start = (this.state.nowPage - 1) * this.state.pageSize
const url = `https://api.douban.com/v2/movie/in_theaters?start=${start}&count=${this.state.pageSize}` fetch(url)
.then(res => res.json())
.then(data => {
this.setState({
isloading: false,
movies: this.state.movies.concat(data.subjects),
totalPage: Math.ceil(data.total / this.state.pageSize)
})
}) /* setTimeout(() => {
this.setState({
isloading: false,
movies: require('./test_list.json').subjects,
totalPage: 1
})
}, 1000) */
} // 渲染电影列表的方法
renderList = () => {
if (this.state.isloading) {
return <ActivityIndicator size="large"></ActivityIndicator>
}
return <FlatList
data={this.state.movies}
keyExtractor={(item, i) => i} // 解决 key 问题
renderItem={({ item }) => this.renderItem(item)} // 调用方法,去渲染每一项
ItemSeparatorComponent={this.renderSeparator} //渲染分割线的属性方法
onEndReachedThreshold={0.5} // 距离底部还有多远的时候,触发加载更多的事件
onEndReached={this.loadNextPage} // 当距离不足 0.5 的时候,触发这个方法,加载下一页数据
/>
} // 渲染每项电影
renderItem = (item) => {
return <TouchableHighlight underlayColor="#fff" onPress={() => { Actions.moviedetail({ id: item.id }) }}>
<View style={{ flexDirection: 'row', padding: 10 }}>
<Image source={{ uri: item.images.small }} style={{ width: 100, height: 140, marginRight: 10 }}></Image>
<View style={{ justifyContent: 'space-around' }}>
<Text><Text style={styles.movieTitle}>电影名称:</Text>{item.title}</Text>
<Text><Text style={styles.movieTitle}>电影类型:</Text>{item.genres.join(',')}</Text>
<Text><Text style={styles.movieTitle}>制作年份:</Text>{item.year}年</Text>
<Text><Text style={styles.movieTitle}>豆瓣评分:</Text>{item.rating.average}分</Text>
</View>
</View>
</TouchableHighlight>
} // 渲染分割线
renderSeparator = () => {
return <View style={{ borderTopColor: '#ccc', borderTopWidth: 1, marginLeft: 10, marginRight: 10 }}></View>
} // 加载下一页
loadNextPage = () => {
// 如果下一页的页码值,大于总页数了,直接return
if ((this.state.nowPage + 1) > this.state.totalPage) {
return
} this.setState({
nowPage: this.state.nowPage + 1
}, function () {
this.getMoviesByPage()
})
}
}

电影详情

import React, { Component } from 'react'

import { View, Image, Text, ActivityIndicator, ScrollView } from 'react-native'

export default class MovieDetail extends Component {
constructor(props) {
super(props)
this.state = {
movieInfo: {}, // 电影信息
isloading: true
}
} componentWillMount() {
fetch('https://api.douban.com/v2/movie/subject/' + this.props.id)
.then(res => res.json())
.then(data => {
this.setState({
movieInfo: data,
isloading: false
})
})
} render() {
return <View>
{this.renderInfo()}
</View>
} renderInfo = () => {
if (this.state.isloading) {
return <ActivityIndicator size="large"></ActivityIndicator>
}
return <ScrollView>
<View style={{ padding: 4 }}>
<Text style={{ fontSize: 25, textAlign: 'center', marginTop: 20, marginBottom: 20 }}>{this.state.movieInfo.title}</Text> <View style={{ alignItems: 'center' }}>
<Image source={{ uri: this.state.movieInfo.images.large }} style={{ width: 200, height: 280 }}></Image>
</View> <Text style={{ lineHeight: 30, marginTop: 20 }}>{this.state.movieInfo.summary}</Text>
</View>
</ScrollView>
}
}

react-native构建基本页面4---渲染电影列表的更多相关文章

  1. 从零学React Native之03页面导航

    之前我们介绍了RN相关的知识: 是时候了解React Native了 从零学React Native之01创建第一个程序 从零学React Native之02状态机 本篇主要介绍页面导航 上一篇文章给 ...

  2. react native (1) 新建页面并跳转

    新建页面 1.新建文件 import React from 'react'; import { Text } from 'react-native'; export default class tod ...

  3. react native tap切换页面卡顿

    问题描述:做一个页面,左边是导航,每次点击一个菜单,右边立即显示出对应的视图,数据会重新过滤,使用setState 更新视图,会卡顿 解决办法: InteractionManager.runAfter ...

  4. React Native 中 跨页面间通信解决方案之 react-native-event-bus

    https://github.com/crazycodeboy/react-native-event-bus 用法: A页面和B页面中都有相同的列表,点击B页面中的收藏按钮,A页面会跟着更新 impo ...

  5. React Native登录注册页面实现空白处收起键盘

    其实很简单,直接使用ScrollView作为父视图即可.有木有很神奇啊,以前都还不知道呢.....

  6. 【React Native】某个页面禁用物理返回键

    1.引入组件 import { BackHandler, } from 'react-native'; 2.添加监听 componentDidMount(): void { BackHandler.a ...

  7. react 项目实战(五)渲染用户列表

    现在我们需要一个页面来展现数据库中记录的用户. 在/src/pages下新建UserList.js文件. 创建并导出UserList组件: import React from 'react'; cla ...

  8. React Native 简介:用 JavaScript 搭建 iOS 应用 (1)

    [编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...

  9. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

随机推荐

  1. CentOS7.x以上版本配置DNS失效解决办法

    这2周做实验,centos7.x经常出现yum安装软件包的时候找不到解析地址,提示如下错误 正在尝试其它镜像. Error downloading packages: pam-devel-1.1.8- ...

  2. 【重新整理】log4j 2的使用

    一 概述 1.1 日志框架 日志接口(slf4j) slf4j是对所有日志框架制定的一种规范.标准.接口,并不是一个框架的具体的实现,因为接口并不能独立使用,需要和具体的日志框架实现配合使用(如log ...

  3. 杭电--1009 C语言实现

    思路:要用有限的猫粮得到最多的javabean,则在房间中得到的javabean比例应尽可能的大. 用一个结构体,保存每个房间中的javabean和猫粮比例和房间号,然后将结构体按比例排序,则从比例最 ...

  4. 部署LAMP环境搭建一个网站论坛平台

    修改主机名 Hostname openstack-001 Hostname Login 修改本地域名解析 Vi /etc/hosts 最后一行添加 192.168.1.56 openstack-001 ...

  5. mysql出现 Unknown column 'Password' in 'field list'

    linux安装了mysql之后初始化密码获取:出现了下面的内容,密码很尴尬,无法用root登录: grep 'temporary password' /var/log/mysqld.log [Note ...

  6. codewars--js--Two Joggers--求最小公倍数、最大公约数

    问题描述: Two Joggers Description Bob and Charles are meeting for their weekly jogging tour. They both s ...

  7. 正则表达式验证IP地址(绝对正确)

    正则验证合法_有效的IP地址(ipv4/ipv6) 不墨迹直接上代码: 正则表达式: /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[ ...

  8. 1.3.6 详解build.gradle文件——Android第一行代码(第二版)笔记

    不同于Eclipse,Android Studio是采用Gradle来构建项目的.Gradle是一个非常先进的项目构建工具,它使用了一种基于Groovy的领域特定语言(DSL)来声明项目设置. 首先看 ...

  9. 在服务器上搭建远端git仓库

    推荐使用运行Liunx的机器 请获取root权限后进行下面操作 安装git # 检查是否安装了git如果有版本号就无需再安装 git -v # 安装git sudo apt-get install g ...

  10. JS数据类型和堆栈+变量比较和值的复制+参数传递和类型检测

    变量命名 变量名:字母 数字 下划线 美元符$ jquery:  $     $.each()   $ === jQuery underscore( js的一个函数库)  :   _     _.ea ...