码迷,mamicode.com
首页 > 其他好文 > 详细

FlatList列表组件的使用

时间:2020-04-30 17:05:31      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:color   add   消失   lex   line   ram   enum   image   inf   

PaymentChannelScreen.js

/**
 *Created by lixingru on 2020/4/14
 */
import React, { Component, PureComponent } from ‘react‘;
import {
    StyleSheet,
    Text,
    Image,
    View,
    ScrollView,
    FlatList,
    TouchableOpacity,
    Dimensions
} from ‘react-native‘;
import {Cruserinfo} from "../child/ChargeRecordView"
import LoadingView from "../../../../component/Loading";
import * as api from "../../../../networking/Api";

//绑定银行卡列表
class PaymentChannelItemView extends Component {
    _onPress = ()=>{
        const { ChannelItem ,navigation} = this.props
        navigation.navigate(‘selectBank‘,{ data: {
            ChannelItem:ChannelItem,
            customerId:this.props.navigation.state.params.data.customer_id,
            nameAndIdCard:this.props.navigation.state.params.data,
            phoneNumber:this.props.navigation.state.params.data.phoneNumber

        }})
    }

    render() {
        const { channelPayName } = this.props.ChannelItem
        return (
            <View style={styles.Channel}>
                <TouchableOpacity style={styles.ChannelItem} onPress={this._onPress}>
                    <Text style={{fontSize:18}}>{channelPayName}</Text>
                </TouchableOpacity>
            </View>
        )
    }
}

const windowHeight = Dimensions.get(‘window‘).height;
export default class PaymentChannelScreen extends Component {
    constructor() {
        super(...arguments)
        this.state = {
            isLoading:false,
            data: []
        }
    }

    componentDidMount(){
        //一进入页面请求数据并展示列表
        this.fetchData()      
        console.log(‘+_+_+_:‘,this.props.navigation.state.params)
    }

    //渲染列表子项
    _renderItem = ({ item, index }) => {
        return ( 
            <PaymentChannelItemView navigation={this.props.navigation} 
            ChannelItem={item}></PaymentChannelItemView>
        )
    }

    // 刷新数据
    fetchData = async () => {
        // 开始请求数据时显示加载控件
        this.setState({
            isLoading: true,
        })
        const userId =  await EDMoney.getUserId()
        api.selectAllCapitalChannel(userId)
        .then((res) => {
            //请求成功后刷新数据
            this.setState({
                data: res.data,
            })
            //请求成功0.4秒后,刷新控件消失
            setTimeout(()=>{
                this.setState({
                    isLoading: false,
                })
            },400); 
        })
        .catch((error) => {
            //请求失败0.4秒后,刷新控件消失
            setTimeout(()=>{
                this.setState({
                    isLoading: false,
                })
            },400); 
            if (error.errors) {
                EDMoney.Toast.show(error.errors._error)
            } else {
                EDMoney.Toast.show(‘网络错误‘)
            }
        })
    }

    /*没有数据时显示的组件*/
    listEmptyComponent() {
        return <View style={{backgroundColor: ‘#fff‘, flex: 1,height:windowHeight}}>
                    <Text style={{
                        alignItems: ‘center‘,
                        textAlign: ‘center‘,
                        color: ‘#e6e6e6‘,
                        fontSize:18,
                        lineHeight:windowHeight-75
                    }}
                    >
                    暂无授权信息
            </Text>
        </View>
    }
    render() {
        return (
            <View style={styles.root}>
                <View style={styles.list}>
                    <FlatList
                            data={this.state.data}
                            extraData={this.state}
                            initialNumToRender={10}
                            keyExtractor={(item, index) => item.key}
                            renderItem={this._renderItem}
                            ListEmptyComponent={this.listEmptyComponent} // 没有数据时显示的界面
                            refreshing={this.state.isLoading}// 是否刷新 ,属自带刷新控件,refreshing值为true时显示刷新控件
                            onRefresh={()=>{this.fetchData()}}// 刷新方法,写了此方法,下拉才会出现  刷新控件,使用此方法必须写 refreshing
                            // ListFooterComponent={this._renderFooter()}
                        />
                </View>
            </View>
        )
    }
}



const styles = StyleSheet.create({
    root:{
        flex: 1,
        backgroundColor:‘#f0f0f0‘
    },
    list:{
        flex: 1,
    },
    Channel:{
        borderTopWidth:1,
        borderTopColor:‘#e6e6e6‘,
        marginBottom: 6
    },
    ChannelItem:{
        height:60,
        backgroundColor:‘#fff‘,
        paddingLeft:10,
        borderBottomWidth:1,
        borderBottomColor:‘#e6e6e6‘,
        borderStyle:‘solid‘,
        justifyContent: ‘center‘,
        alignItems: ‘center‘,
        
    }
})

 

FlatList列表组件的使用

标签:color   add   消失   lex   line   ram   enum   image   inf   

原文地址:https://www.cnblogs.com/itgezhu/p/12809726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!