React Native 實現列表抽屜式效果

進入項目的跟路徑添加抽屜庫 npm install react-native-drawer --save


B2C9EA39-FDC7-4AB6-9338-0D76BC9BE1C3.png

//實現抽屜式效果的代碼方式如下:

 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
  Dimensions,
  ScrollView,
  ListView,
  Image
} from 'react-native';
import Drawer from 'react-native-drawer';
var Arr = ['我的賬戶','轉賬匯款','投資理財','余額理財','工商e支付','手機充值','e繳費','信用卡','注冊賬戶轉賬','貸款','融e購','融e聯','Apple Pay'];
var ImageArr = [require('./1.jpg'),require('./2.jpg'),require('./3.jpg'),require('./4.jpg'),require('./5.jpg')];
const {width,height} = Dimensions.get('window');

export default class MyAnimated extends Component {
  constructor(props) {
    super(props);
  
    this.state = {
        openType:false
    };
  }
  //接收子組件傳來的數據改變openType狀態,刷新UI
  LeftClicked(openType){
     this.setState({
         openType:openType
     });
  }
  //側拉的實現方式
  render() {
    return (
      <View style={styles.container}>
       <Drawer type='overlay'
               side='left'
               content={<LeftVC />} //左側拉的頁面
               tapToClose={true}
               panOpenMask={0.2}
               panDrawerOffset={0.2}
               panCloseMask={0.2}
               closedDrawerOffset={0}
               open={this.state.openType}
               style={drawerStyles}
               tweenHandler={(ratio)=>({main:{opacity:(2-ratio)/2}})}>
              <Main LeftClicked={this.LeftClicked.bind(this)}/>
       </Drawer>
      </View>
    );
  }
}
// 左邊側拉欄代碼實現
class LeftVC extends Component{
    render(){
        return(
         <View style={{flex:1,}}>
          <View style={{width:width-100,height:64,backgroundColor:'#3893C9',alignItems:'center'}}></View>
            <View style={{width:width-100,backgroundColor:'white',flex:1}}>
                 <View style={{height:40,
                               justifyContent: 'center',
                               alignItems: 'center',
                               backgroundColor:'orange',
                               marginTop:10}}><Text>我的錢包</Text></View>
                <View style={{height:40,
                               justifyContent: 'center',
                               alignItems: 'center',
                               backgroundColor:'orange',
                               marginTop:10}}><Text>我的卡卷</Text></View>
            </View>
         </View>
      );
    }
}
// 首頁代碼列表
class Main extends Component{
   constructor(props) {
    super(props);
  
    this.state = {
        dataSource:new ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2}).cloneWithRows(Arr),
    };
  }
  //cell列表
  cellClicked(data){
    return(
       <View style={{height:50,justifyContent: 'center',borderWidth:1}}>
         <Text>{data}</Text>
       </View>
    );
  }
  //滾動視圖放在SectionHeader
  renderSectionHeader(){
    return(
        <View style={{height:120,backgroundColor:'orange'}}>
            <ScrollView contentContainerStyle={{width:Dimensions.get('window').width*5,height:120}}
                        horizontal={true}
                        showsVerticalScrollIndicator={true}
                        pagingEnabled={true}
                        bounces={false}>
                {this.renderImageView()}
            </ScrollView>
        </View>
     );
  }
  // 點擊圖片彈出點擊的圖片個數
  alertClicked(num){
    alert(num);
  }
  // for循環輸出多個圖片
  renderImageView(){
       var allChild = [];
        for(var i=0; i<5; i++){
           allChild.push(
             <TouchableOpacity key={i}  onPress={this.alertClicked.bind(this,i)}>
                 <Image 
                   key={i} 
                   style={{width: Dimensions.get('window').width, height: 120}}
                   source={ImageArr[i]}
                   resizeMode={Image.resizeMode.stretch}
                />
             </TouchableOpacity>
           )
        }
        return allChild;
  }
  //側拉方法傳入首頁側拉刷新UI
  LeftClicked(){
    this.props.LeftClicked(true);
  }
    render(){
      return(
         <View style={{flex:1,backgroundColor:'orange'}}>
            <View style={{height:64,backgroundColor:'#3893C9',flexDirection:'row'}}>
                <TouchableOpacity style={{flex:1}} onPress={()=>{this.LeftClicked()}}>
                     <Text style={{marginTop:30,fontSize:20,color:'white'}}>菜單</Text>
                </TouchableOpacity>
                <View style={{flex:1,alignItems: 'center',}}><Text style={{marginTop:30,fontSize:20,color:'white'}}>首頁</Text></View>
                <View style={{flex:1}}></View>
             </View>
             <ListView style={{flex:1}}
                       dataSource={this.state.dataSource}
                       renderRow={this.cellClicked.bind(this)}
                       renderHeader={this.renderSectionHeader.bind(this)}
              />
         </View>
      );
   }
}
const drawerStyles = {
  drawer:{ 
     shadowColor:'#000000', 
     shadowOpacity:0.8, 
     shadowRadius:3},
     main:{
        paddingLeft:3
     },
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#EFF3F6',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('MyAnimated', () => MyAnimated);

效果圖如下

QQ20170818-092402-HD.gif
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,814評論 25 708
  • 簡短說明 收錄一些好用的RN第三方組件,以方便日常的使用,大家有什么推薦的也可以跟我說,我加進去。如有冒犯,可以聯...
    以德扶人閱讀 43,686評論 44 214
  • 盡管在移動開發中,原生APP的開發成本很高,但現階段基于原生開發仍然是必須的,因為Web的用戶體驗仍無法超越Nat...
    奔跑的大橙子閱讀 5,416評論 0 11
  • 2017年10月10號 星期二 多云 今天下午放學媽媽帶著我去接妹妹,接妹妹的路上我遇見了我的同學我和他一起玩...
    室是陋室閱讀 2,553評論 0 1
  • 我叫李亮,剛剛考上研究生,回憶起大學四年的生活過的太單調,生活里除了學習就是學習,別人去旅游我在學習,別人談戀愛我...
    牧鋒閱讀 832評論 0 0