react-native构建基本页面4---渲染电影列表
电影列表
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---渲染电影列表的更多相关文章
- 从零学React Native之03页面导航
之前我们介绍了RN相关的知识: 是时候了解React Native了 从零学React Native之01创建第一个程序 从零学React Native之02状态机 本篇主要介绍页面导航 上一篇文章给 ...
- react native (1) 新建页面并跳转
新建页面 1.新建文件 import React from 'react'; import { Text } from 'react-native'; export default class tod ...
- react native tap切换页面卡顿
问题描述:做一个页面,左边是导航,每次点击一个菜单,右边立即显示出对应的视图,数据会重新过滤,使用setState 更新视图,会卡顿 解决办法: InteractionManager.runAfter ...
- React Native 中 跨页面间通信解决方案之 react-native-event-bus
https://github.com/crazycodeboy/react-native-event-bus 用法: A页面和B页面中都有相同的列表,点击B页面中的收藏按钮,A页面会跟着更新 impo ...
- React Native登录注册页面实现空白处收起键盘
其实很简单,直接使用ScrollView作为父视图即可.有木有很神奇啊,以前都还不知道呢.....
- 【React Native】某个页面禁用物理返回键
1.引入组件 import { BackHandler, } from 'react-native'; 2.添加监听 componentDidMount(): void { BackHandler.a ...
- react 项目实战(五)渲染用户列表
现在我们需要一个页面来展现数据库中记录的用户. 在/src/pages下新建UserList.js文件. 创建并导出UserList组件: import React from 'react'; cla ...
- React Native 简介:用 JavaScript 搭建 iOS 应用 (1)
[编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...
- React Native初探
前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...
随机推荐
- css架构技巧
1. 写一个reset.css 用于清除浏览器标签默认样式并定义全局样式,这样就不会因为浏览器默认样式出现问题,因为不同浏览器的默认样式还是不一样的
- Literature Review: Improving Image-Based Localization by Active Correspondence Search
Abstract Input: A query image Source: A point cloud reconstruction of a large scene (有一百多万3D点) Resul ...
- Arm开发板+Qt学习之路-multiple definition of
问题描述:在一个头文件a.h中定义一些变量x,在其他.c文件中(b.c,c.c)要用到.用一般的全局变量的方法,编译时总是提示error:multiple definition of x 问题分析:o ...
- 【GET TIPS】Chrome所见即所得的截图技巧
精简的前言: 对,我就是想说下事情的来龙去脉.您要不想听,就把耳朵捂起来23333. 想截个新冠肺炎病毒,全国确诊人数今日增长的图,以确定非湖北地区不再明显增长. 但由于网页需要的内容分布在两页,需要 ...
- Django request对象与ORM简介
form表单 form表单默认是以get请求提交数据的 http://127.0.0.1:8000/login/?username=admin&password=123 action参数 1. ...
- c++输入输出,保留几位小数
#include <iomanip> //头文件 //第一种写法 cout<<setiosflags(ios::); //第二种写法 cout.setf(ios::fixed) ...
- python中class的定义及使用
#类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法. #对象:它是类的实例化. #方法:类中定义的函数. #类(Class) 由3个部分构成: ...
- kettle文本文件写入数据库,简单进行数据清洗
使用kettle7.0,java8.0,Navicat,实验数据使用全国肺炎2月24日的数据 1.建立关系 2.创建连接 如果是第一次连接,可能会出现连接不上的情况,这时候可能情况是没有将Mysql的 ...
- C#中的异步编程--探索await与async关键字的奥妙之处,原来理解和使用异步编程可以这么简单
前言 await与async是C#5.0推出的新语法,关于await与async有很多文章讲解.但看完后有没有这样一种感觉,感觉这东西像是不错,但好像就是看不太懂,也不清楚该怎么使用.虽然偶有接触,但 ...
- PS_0001:改变图片颜色 填充颜色
1,创建新图存 ctrl + j 2,点击前景色按钮,改变颜色 3,前景色的键盘快捷键是“Alt+Delete”,背景色的键盘快捷键是“Ctrl+Delete”