電影串列
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 =https://www.cnblogs.com/ygjzs/p/> {
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=https://www.cnblogs.com/ygjzs/p/{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
電影詳情
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 =https://www.cnblogs.com/ygjzs/p/> {
this.setState({
movieInfo: data,
isloading: false
})
})
}
render() {
return
{this.renderInfo()}
}
renderInfo = () => {
if (this.state.isloading) {
return
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/154872.html
標籤:JavaScript
