React-Native学习之路(12) - 用FlatList实现留言板评论功能 +(解决键盘跳出,顶出tabNavigator的问题)

(1) 知识点:

  • (1) concat():用于连接两个或多个数组,返回一个新的数组。
   var item = [{
                            avatar1: "http://dummyimage.com/200x200/f2c779)",
                            date1: "1994-04-20",
                            key: "120000199" + i +"31207725X",
                            name1: "Cynthia Perez",
                            content1:this.state.content
                        }].concat(liuyanContent)   

                        i++;
// liuyanContent是get请求得到的数据,格式和上面的一样,
// 只是 content内容不一样,而且key要不一样,这里用i++得到不同的key
  • (2) setState是异步的,所以要用回调函数立即取得新的状态值后,要做什么。。
this.setState({
    _isSending:true
}, () => {
    回调,更新状态后要做的事情
})
  • (2) FlatList的数据源格式:
 {item.key}}
/>
----------------------------
可以看到:在flatlist的data数据源中,是一个对象数组,即
[ 
{ key:'01', name:'woow.wu',avatar:'...'} , 
{ key:'02', name:'shengjudao',avatar:'...'}
]
---------------------------
可以看到:在renderItem中,带有参数item,这里要打印item对象的数据结构
   _renderItemHuiFu(data) {
        // console.log(data)
        // liuyan = data
      return(
       { data.item.name}
      )
  • (3) get请求,得到已评论的数据
 _getDataHuiFu() {
        fetch('http://rapapi.org/mockjs/22101/api/pinglun/huifu')
            .then( (response) => response.json() )
            .then( (data) => {
                // console.log(data)
                var Mock = MOCK.mock(data).huifulist;
                // console.log(Mock)
                // var mockDataArray = [];
                // mockDataArray = Mock.fuhuilist;
                // console.log(mockDataArray)
                this.setState({
                    huifu: Mock
                })

            } )
            .catch( (err)=> {
                console.error(err)
            })
    }
  • (3) post请求,提交留言的内容,返回seuccess布尔值,为true

  • (4) 提交按钮和留言TextInput框

                    
                         {
                                this.setState({
                                    content: text
                                })
                            } }
                        >
                        

                        
                            
                                提 交
                            
                        
                    
  • (5) 提交函数:
 _submit() {
        if(!this.state.content) {
            return alert('留言不能为空')
        }
        if( this.state._isSending){
            return alert('评论正在提交')
        }
        this.setState({
            _isSending:true
        },() => {
            fetch('http://rapapi.org/mockjs/22101/api/pinglun/huifupost',{
                method:'POST',
                headers: {
                    'Accept':'application/json',
                    'Content-Type':'application/json'
                },
                body:JSON.stringify({
                    'content':this.state.content
                })
            }).then( (response)=> response.json() )
                .then( (data) => {
                    if( data && data.success){
                        var liuyanContent = this.state.huifu;   //get请求得到的对象数组
                        // liuyanContent.map( (item) => {
                        //     ArrayHuiFu.push({
                        //         key: g,
                        //         data: item
                        //     })
                        //     g++;
                        // })
                        // console.log(ArrayHuiFu)
                        var item = [{
                            avatar1: "http://dummyimage.com/200x200/f2c779)",
                            date1: "1994-04-20",
                            key: "120000199" + i +"31207725X",
                            name1: "Cynthia Perez",
                            content1:this.state.content
                        }].concat(liuyanContent)

                        i++;

                        this.setState({
                            huifu:item,
                            _isSending: false
                        })

                    }
                })
        } )
    }

效果图:


React-Native学习之路(12) - 用FlatList实现留言板评论功能 +(解决键盘跳出,顶出tabNavigator的问题)_第1张图片
效果图

效果图

完整代码:

/**
 * Created by WOOW.WU on 2017/8/1.
 */
/**
 * Created by WOOW.WU on 2017/8/1.
 */
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Dimensions,
    ScrollView,
    Image,
    FlatList,
    TextInput,
    TouchableHighlight
} from 'react-native';

const { width, height } = Dimensions.get('window');
import Icon from'react-native-vector-icons/FontAwesome';
import MOCK from 'mockjs'
// pp2={
//     avatar:'http://dummyimage.com/600x300/f7d8d3)'
// }

var i=3;
var ArrayHuiFu = [];
// var liuyan = {};
export default class ff extends Component {
    constructor(props){
        super(props);
        this.state = {
            ping: {},
            huifu: [],
            content:'',
            _isSending: false
        }
    }

    componentDidMount() {
        this._getData()
        this._getDataHuiFu()
    }

    _getData() {
        fetch('http://rapapi.org/mockjs/22101/api/pinglun')
            .then( (response) => response.json() )
            .then( (data) => {
                var pp = MOCK.mock(data).pinglist;
                // var pp2 = MOCK.mock(data);
                // console.log(pp)
                // console.log(pp2)

                this.setState({
                    ping: pp,
                    // ping2: pp2
                })
            } )
            .catch( (err)=> {
                console.error(err)
            })
    }

    _getDataHuiFu() {
        fetch('http://rapapi.org/mockjs/22101/api/pinglun/huifu')
            .then( (response) => response.json() )
            .then( (data) => {
                // console.log(data)
                var Mock = MOCK.mock(data).huifulist;
                // console.log(Mock)
                // var mockDataArray = [];
                // mockDataArray = Mock.fuhuilist;
                // console.log(mockDataArray)
                this.setState({
                    huifu: Mock
                })

            } )
            .catch( (err)=> {
                console.error(err)
            })
    }


    _renderItemHuiFu(data) {
        // console.log(data)
        // liuyan = data
      return(
          
              
                  
              

              
                  {data.item.name1}
                  { data.item.content1 }
                  { data.item.date1 }
              


          
      )
    }

    _submit() {
        if(!this.state.content) {
            return alert('留言不能为空')
        }
        if( this.state._isSending){
            return alert('评论正在提交')
        }
        this.setState({
            _isSending:true
        },() => {
            fetch('http://rapapi.org/mockjs/22101/api/pinglun/huifupost',{
                method:'POST',
                headers: {
                    'Accept':'application/json',
                    'Content-Type':'application/json'
                },
                body:JSON.stringify({
                    'content':this.state.content
                })
            }).then( (response)=> response.json() )
                .then( (data) => {
                    if( data && data.success){
                        var liuyanContent = this.state.huifu;
                        // liuyanContent.map( (item) => {
                        //     ArrayHuiFu.push({
                        //         key: g,
                        //         data: item
                        //     })
                        //     g++;
                        // })
                        // console.log(ArrayHuiFu)
                        var item = [{
                            avatar1: "http://dummyimage.com/200x200/f2c779)",
                            date1: "1994-04-20",
                            key: "120000199" + i +"31207725X",
                            name1: "Cynthia Perez",
                            content1:this.state.content
                        }].concat(liuyanContent)

                        i++;

                        this.setState({
                            huifu:item,
                            _isSending: false
                        })

                    }
                })
        } )
    }

    render() {
        // console.log(this.state.huifu)
        // console.log(pp2.avatar)
        // console.log(this.state.ping2)
        // console.log(this.state.ping2.pinglist)
        // console.log(this.state.ping2.pinglist)
        // console.log(this.state.ping2.pinglist.avatar1)

        return (
            
                
                    我的圈子
                    
                

                
                    
                        
                            
                            {this.state.ping.name1}
                        
                        
                            { this.state.ping.content1 }
                        
                        
                            { this.state.ping.date1 }
                        
                    
                    
                         {
                                this.setState({
                                    content: text
                                })
                            } }
                        >
                        

                        
                            
                                提 交
                            
                        
                    
                    
                    


                
            
        );
    }
}

const styles = StyleSheet.create({
    infoxContentContainer: {
        marginTop:10,
        marginBottom:10
    },
    infoxDate: {
        fontSize: 16
    },
    infoxContent: {
        fontSize: 18,
        lineHeight:36
    },
    name1: {
        fontSize: 20
    },
    infoBox: {
        padding:16,
        borderBottomColor: 'rgba(0,0,0,0.1)'
    },
    infoxBoxTop: {
        flexDirection:'row',
        justifyContent:'flex-start',
        alignItems:'center'
    },
    avatar: {
        width:100,
        height:100,
        borderRadius:50,
        marginRight: 10
    },
    myCircleText: {
        fontSize: 18
    },
    myCircle: {
        height: 60,
        width: width,
        backgroundColor: 'rgba(0,0,0,0.1)',
        flexDirection: 'row',
        justifyContent:'space-between',
        alignItems:'center',
        paddingLeft: 20,
        paddingRight: 20
    },
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
    },
});

(2) 键盘跳出,顶出tabnavigator

(详细)http://www.jianshu.com/p/b877115fff1b
http://blog.csdn.net/u011690583/article/details/53808773

你可能感兴趣的:(React-Native学习之路(12) - 用FlatList实现留言板评论功能 +(解决键盘跳出,顶出tabNavigator的问题))