《ReactNative》之下拉选功能

1.效果

    移动开发中有个常见的功能,就是类似于下拉选的功能。下面是一个简单的实现,供大家参考。
    点击头部文字或箭头图标,显示课程选择区域。点击课程或选择区域之外,完成选择,选择区域消失。

          

2.代码

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  ScrollView,
  TouchableOpacity,
  TouchableWithoutFeedback,
  Image,
  ListView,
  Modal ,
} from 'react-native';
var Dimensions = require('Dimensions');
const {width,height} = Dimensions.get('window');
export default class Select extends Component {
  constructor(props){
    super(props);
    this.state=({
      showModal:false,
      course:"语文",
      
    });
  }
  componentWillMount(){
    
  }
  selCourse(course){
    this.setState({
      showModal:false,
      course:course,
    });
  }
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.headStyle}>
          <Text style={styles.headText} onPress={()=>{this.setState({showModal:true})}}>
            {this.state.course}
          </Text>
          <TouchableOpacity style={{marginLeft:10}} 
            onPress={()=>{this.setState({showModal:true})}}
            hitSlop={{top: 15, left: 15, bottom: 15, right: 15}}>
            <Image style={styles.arrowStyle} source={{uri:'arrow_down'}}/>
          </TouchableOpacity>
        </View>
        <Modal  
          visible={this.state.showModal}   
          transparent={true}  
          animationType='fade'   
          onRequestClose={() => {}}  
          style={{flex:1}}  
          ref="modal"  >
          <TouchableWithoutFeedback onPress={()=>{this.setState({showModal:false})}} >
          <View style={{flex:1,alignItems:'center',backgroundColor:'rgba(0, 0, 0, 0.5)',}} 
            
            >
            <TouchableWithoutFeedback onPress={()=>{}}>
              <View style={{backgroundColor:'#fff',width:width,
                justifyContent:'center',
                
              }}
              
              > 
                <View style={styles.headStyle}>
                  <Text style={styles.headText} onPress={()=>{this.setState({showModal:false})}}>
                    {this.state.course}
                  </Text>
                  <TouchableOpacity style={{marginLeft:10}} 
                    onPress={()=>{this.setState({showModal:false})}}
                    hitSlop={{top: 15, left: 15, bottom: 15, right: 15}}>
                    <Image style={styles.arrStyle} source={{uri:'arr_up'}}/>
                  </TouchableOpacity>
                </View>
                <View style={styles.courseWrap}>
                  <CourseItem course="语文" onPress={()=>{this.selCourse('语文')}}/>
                  <CourseItem course="数学" onPress={()=>{this.selCourse('数学')}}/>
                  <CourseItem course="英语" onPress={()=>{this.selCourse('英语')}}/>
                </View>
                <View style={[styles.courseWrap,{marginBottom:10}]}>
                  <CourseItem course="物理" onPress={()=>{this.selCourse('物理')}}/>
                  <CourseItem course="化学" onPress={()=>{this.selCourse('化学')}}/>
                </View>
              </View>
            </TouchableWithoutFeedback>
          </View>
          </TouchableWithoutFeedback>
        </Modal>
      </View>
    );
  }
}
class CourseItem extends Component{
  render(){
    return(
      <TouchableOpacity style={styles.boxView} onPress={this.props.onPress}>
        <View style={{padding:10}}>
          <Text style={{fontSize:18,fontWeight:'bold'}}>{this.props.course}</Text>
        </View>
      </TouchableOpacity>
    )
  }
}
var cols = 3;
var boxW = 70;
var vMargin = (width-cols*boxW)/(cols+1);
var hMargin = 25;
const styles = StyleSheet.create({
   arrStyle:{
    width:26,
    height:26,
    resizeMode:'contain',
  },
  boxView:{
    justifyContent:'center',
    alignItems:'center',
    width:boxW,
    height:boxW,
    marginLeft:vMargin,
    marginTop:hMargin,
    borderWidth:StyleSheet.hairlineWidth,
    borderColor:'#999',
    borderRadius:5,
  },
  courseWrap:{
    flexDirection:'row',
    justifyContent:'flex-start',
    borderWidth:0,
    borderColor:'orange',
  },
  selCourseText:{
    padding:8,
    fontSize:18,
  },
  blackText:{
    color:'black',
    fontSize:16,
  },
  arrowStyle:{
    width:20,
    height:20,
  },
  textWrapView:{
    paddingTop:10,
    paddingBottom:10,
  },
  headText:{
    fontSize:22,
  },
  headStyle:{
    flexDirection:'row',
    width:width,
    justifyContent:"center",
    alignItems:'center',
    backgroundColor:'#F2F2F2',
    paddingTop:15,
    paddingBottom:15,
  },
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: '#fff',
  },
  
});

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值