plateName.js 4.87 KB
/**
 * Created by Cassie on 2018/03/06
 */
import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    Image,
    FlatList,
    StatusBar,
    ActivityIndicator
} from 'react-native';

import {zoomW,zoomH} from '../../utils/getSize';
import {xnBorderWidth,xnToast} from "../../utils/utils";
import AppService from "../../service/AppService";

const back = require('../../img/back_gray.png');
const close = require('../../img/close.png');
const cross = require('../../img/cross.png');
const plate = require('../../img/plate.png');
const arrow = require('../../img/arrow.png');

export default class PlateName extends Component {
    static navigationOptions = ({ navigation, screenProps })=>({
        title: '选择板块',
        headerLeft:(<View style={{display:'flex',flexDirection:'row'}}>
            <TouchableOpacity style={styles.leftIcon} onPress={navigation.state.params?navigation.state.params.back:null}>
                <Image source={back} style={styles.backIcon} resizeMode="contain" />
            </TouchableOpacity>
            <TouchableOpacity activeOpacity={1} style={styles.leftIcon} onPress={()=>{}}>
                <Image style={styles.closeIcon} resizeMode="contain" />
            </TouchableOpacity>
        </View>),
        headerRight:(<View/>)
    });

    constructor(props) {
        super(props);
        this.state = {
            loading:true
        };
    };

    componentWillMount(){
        this.getList();
    };

    componentDidMount(){
        //设置头部
        this.props.navigation.setParams({
            back:()=>{
                this.props.navigation.goBack();
            },

        });
    };

    /*获取板块列表*/
    getList(){
        this.setState({
            loading:true
        });
        AppService.findBoardList({forumId:global.forumId}).then(data=>{
            this.setState({
                loading:false
            });
            if(data.message){
                xnToast(data.message);
                return;
            }
            if(data.errors.length > 0) {
                xnToast(data.errors[0].message);
            }else{
                this.setState({
                    list:data.result
                });
            }
        })
    };
    /* 渲染列表*/
    keyExtractor = (item,index) => index;
    /*首页列表*/
    renderItem = ({item,index}) => (
        <TouchableOpacity style={styles.plateItem} onPress={() => this.selectPlate(item.id,item.name)}>
            <View style={styles.plateName}>
                <Text style={{fontSize:16,color:'#333'}}>{item.name}</Text>
            </View>
        </TouchableOpacity>
    );
    /*选择后返回*/
    selectPlate(id,name){
        const {params} = this.props.navigation.state;
        global.threadId = id;
        global.threadName = name;
        if(params.selectPlate){
            params.selectPlate(id,name);
        }
        this.props.navigation.goBack();
    };

    render(){
        return (
            <View style={styles.background}>
                <StatusBar barStyle="default" />
                {this.state.list && <FlatList keyExtractor={this.keyExtractor} data={this.state.list} renderItem={this.renderItem} numColumns={2} />}
                {this.state.loading && <View style={styles.loadingBg}>
                    <ActivityIndicator size="large" />
                </View>}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    leftIcon:{
        width:(50/zoomW),
        height:'100%',
        display:'flex',
        flexDirection:'row',
        alignItems:'center'
    },
    backIcon:{
        width:(16/zoomW),
        height:(16/zoomH),
        marginLeft:(10/zoomW)
    },
    closeIcon:{
        width:(16/zoomW),
        height:(16/zoomH)
    },
    background:{
        flex:1,
        backgroundColor:'#f2f2f2',
        paddingLeft:(10/zoomW),
        paddingRight:(10/zoomW),
        flexDirection:'row',
        flexWrap:'wrap',
        paddingTop:(10/zoomW)
    },
    plateItem:{
        width:'50%',
        display:'flex',
        justifyContent:'center',
        alignItems:'center',
        marginTop:(5/zoomH),
        marginBottom:(5/zoomH)
    },
    plateName:{
        width:(170/zoomW),
        height:(40/zoomH),
        borderWidth:xnBorderWidth(),
        borderColor:'#ddd',
        borderStyle:'solid',
        display:'flex',
        justifyContent:'center',
        alignItems:'center',
        borderRadius:(4/zoomW),
        backgroundColor:'#fff'
    },
    rightItem:{
        height:'100%',
        borderLeftWidth:xnBorderWidth(),
        borderLeftColor:'#eee',
        borderStyle:'solid',
        display:'flex',
        justifyContent:'center',
        paddingLeft:(10/zoomW)
    },
    loadingBg:{
        width:'100%',
        height:'100%',
        position:'absolute',
        display:'flex',
        alignItems:'center',
        justifyContent:'center'
    }
});