React Native 實(shí)現(xiàn)分組展開收縮效果

實(shí)現(xiàn)思路:

  1. 將分組cell封裝成一個(gè)單獨(dú)的組件,代碼如下
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  TouchableOpacity
} from 'react-native';
export default class CoverageCell extends Component {
  //初始化數(shù)據(jù),添加isShow屬性,isShow屬性控制展開與關(guān)閉
 constructor(props) {
    super(props);
  
    this.state = {
      isShow:false,
    };
  }

  detail=(title)=>{
// 改變狀態(tài),刷新組件
    this.setState({
      isShow:!this.state.isShow
    });
// 子組件給父組件傳點(diǎn)擊的是哪一個(gè)分組
    this.props.detail(title);
  }
//初始化數(shù)據(jù)需要一個(gè)分組的titil和一個(gè)分組下的數(shù)組
  static defaultProps = {
    title:'',
    cars:[],
  }
//利用push輸出分組下的cell
  isShowText(){
   const {title,cars} = this.props;
   var allChild = [];
   for (var i = 0; i < cars.length; i++) {
     allChild.push(
        <View key={i} style={{height:30,flexDirection:'row',alignItems:'center'}}>
           <Text style={{flex:2}}>{cars[i].name}</Text>
           <Text style={{flex:2,textAlign:'right'}}>{cars[i].phoneNumber}</Text>
        </View>
      )
    }
    return allChild;
  }
  
  render() {
    const {title,cars} = this.props;
    return (
       <View style={{flex: 1}}>
             <View style={{flex:1,marginBottom:15,backgroundColor:'white'}}>
              <TouchableOpacity onPress={()=>{this.detail(title)}}
                                style={{
                                     height:40,
                                     borderColor:'black',
                                     borderWidth:1,
                                     flexDirection:'row',
                                     alignItems:'center'}}>
                 <Text>{title}</Text>
                 <View style={{backgroundColor:this.state.isShow?'red':'green',height:15,width:15}}></View>
              </TouchableOpacity>
              {this.state.isShow?<View>{this.isShowText()}</View>:<View></View>}
             </View>
               
       </View>
    );
  }
}

const styles = StyleSheet.create({
    headerLine:{  
    height:50,  
    flexDirection:'row',  
    borderBottomWidth:1,  
    borderBottomColor:'red',  
  },  
  headerRows:{  
    flex:1,  
    justifyContent:'center',  
    alignItems:'center',  
  },  
  showitemContain:{  
    borderWidth:1,  
    borderColor:'red',  
    height:110,  
    justifyContent:'center',  
    alignItems:'center',  
  }  
});

2.在頁面中引用

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  ListView
} from 'react-native';
import CoverageCell from './CoverageCell';
// 數(shù)據(jù)源
var CoverageArrs = 
          [{title:'家人',persons:[
                 {name:'爸爸',phoneNumber:'13500000001',id:'1'},
                 {name:'媽媽',phoneNumber:'13500000002',id:'2'},
                 {name:'姐姐',phoneNumber:'13500000003',id:'3'},
                 {name:'姐姐',phoneNumber:'13500000004',id:'3'}]},
           {title:'同學(xué)',persons:[
                 {name:'張敏',phoneNumber:'18700000001',id:'4'},
                 {name:'劉聰',phoneNumber:'18700000002',id:'5'},
                 {name:'張瀾',phoneNumber:'18700000003',id:'6'}]},
           {title:'朋友',persons:[
                 {name:'李斯',phoneNumber:'15800000007',id:'7'},
                 {name:'張娃',phoneNumber:'15800000008',id:'8'},
                 {name:'納斯',phoneNumber:'15800000009',id:'9'}]},
           {title:'同事',persons:[
                 {name:'張超',phoneNumber:'13700000003',id:'10'},
                 {name:'康米',phoneNumber:'13700000004',id:'11'},
                 {name:'杜康',phoneNumber:'13700000005',id:'12'},
                 {name:'張楠',phoneNumber:'13700000008',id:'12'},
                 {name:'楊林',phoneNumber:'13400000009',id:'13'}]}
          ];
export default class SectionDemo extends Component {
  constructor(props) {
    super(props);
  
    this.state = {
       dataSource:new ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2}).cloneWithRows(CoverageArrs),
    };
  }
  detail(title){
    alert(title);
  }
  //引用組件把數(shù)組傳進(jìn)去
  renderMover(data){
    const {title,persons} = data;
    return(
        <CoverageCell title={title}
                      cars={persons}
                      detail={this.detail.bind(this)}/>
    );
  }
  render() {
    return (
      <View style={styles.container}>
        <ListView  style={{flex:1,marginTop:64}}
                    dataSource={this.state.dataSource}
                    renderRow={this.renderMover.bind(this)}/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('SectionDemo', () => SectionDemo);
QQ20170810-173411-HD.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,050評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,765評論 25 709
  • 你說這草廬天寒地凍四面漏風(fēng),可哪有夜晚慘呢?當(dāng)黑夜降臨的時(shí)候,你連一顆星星都找不到。 就像你總覺得自己多可憐,連一...
    貝龍閱讀 650評論 28 4
  • 參考內(nèi)容:python 科學(xué)計(jì)算 發(fā)行版 Anaconda安裝使用教程深度學(xué)習(xí)框架Keras的安裝官方資料:Ana...
    小異_Summer閱讀 960評論 0 1
  • 嗨,還記得孔子的《大道之行也》嗎?初中那會兒,老師每每要求我們背文言文,對我這種不喜歡死記硬背的人來說,那都是一把...
    龔紫妍閱讀 4,201評論 0 2

友情鏈接更多精彩內(nèi)容