ReactNative 上拉加载和下拉刷新模板

import React,{Component} from 'react'
import {
  View,
  Text,
  Image,
  StyleSheet,
  FlatList,
  ActivityIndicator
} from 'react-native'

class Movie extends Component{
    state={
        arr:[
            'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1856129457,2351794353&fm=26&gp=0.jpg',
            'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1856129457,2351794353&fm=26&gp=0.jpg',
            'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1856129457,2351794353&fm=26&gp=0.jpg',
            'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2207260983,3766756222&fm=26&gp=0.jpg',
            'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2207260983,3766756222&fm=26&gp=0.jpg',
            'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2207260983,3766756222&fm=26&gp=0.jpg'    
        ],
        loading:false
    }
    _renderItem({item,index})
    {
        console.log(item);
        return(
            <View style={styles.item}>
                <Image source={{uri:item}} style={styles.image}/>
                <Text>电影名称</Text>
            </View>
        )
    }
    divider()
    {
        return (
            <View style={styles.divider}>

            </View>
        )
    }
    footer()
    {
        return (
            <ActivityIndicator size="large" color="#ccc" animating={this.state.loading}/>
        )
    }   
    loadMore()
    {
        this.setState({
            loading:true
        })
        this.setState({
            arr:this.state.arr.concat(this.state.arr)
        })
        this.setState({
            loading:false
        })
    }
    refresh()
    {
        this.setState({
            loading:true
        })
        this.setState({
            arr:[
                'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1856129457,2351794353&fm=26&gp=0.jpg',
                'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1856129457,2351794353&fm=26&gp=0.jpg',
                'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1856129457,2351794353&fm=26&gp=0.jpg',
                'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2207260983,3766756222&fm=26&gp=0.jpg',
                'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2207260983,3766756222&fm=26&gp=0.jpg',
                'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2207260983,3766756222&fm=26&gp=0.jpg'    
            ]
        })
        this.setState({
            loading:false
        })
    }
    emptyLoad()
    {
        return (
            <Text>数据稍后就来</Text>
        )
    }
    render()
    {
        return(
            <View>
                <FlatList
                    style={styles.wrap}
                    data={this.state.arr}
                    renderItem={this._renderItem.bind(this)}
                    numColumns={2}
                    keyExtractor={(item,index)=>item+index} 
                    columnWrapperStyle={{justifyContent:'space-around',marginBottom:20}}
                    ItemSeparatorComponent={this.divider}
                    ListFooterComponent={this.footer.bind(this)}
                    // 上拉加载
                    onEndReachedThreshold={0.2}
                    onEndReached={this.loadMore.bind(this)}
                    // 下拉刷新
                    refreshing={this.state.loading}
                    onRefresh={this.refresh.bind(this)}
                    // 列表为空时加载
                    ListEmptyComponent={this.emptyLoad}

                />
                
            </View>
        )
    }
}

const styles = StyleSheet.create({
    wrap:{
        paddingHorizontal:10,

    },
    item:{
        width:'47%',
        justifyContent:'center',
        alignItems:'center',
        
    },
    image:{
        height:200,
        width:'100%',
        marginBottom:10
    },
    divider:{
        width:'100%',
        height:2,
        backgroundColor:'#ccc',
        marginBottom:5
    }


})

export default Movie
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值