ReactNative进阶之评分控件的封装
来源:互联网 发布:可米酷漫画软件 编辑:程序博客网 时间:2024/06/05 18:16
评分控件在原生开发中使用很广泛,android中叫做RatingBar,使用方式极其easy,不会用的可以单独找我私聊。今天的主题是使用reactnative来实现RatingBar的效果。
import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View,Dimensions,Image,TouchableOpacity,TouchableWithoutFeedback} from 'react-native';var {width, height} = Dimensions.get('window'); var widthSize;var heightSize;var selecteIcon ;var unselectIcon; var totalScore; var currentScore ;var callBack;var selected;export default class RattingView extends Component{ constructor(props) { super(props); var containerStyle = this.props.style; var config = this.props.config; callBack= this.props.callback; if(config != undefined){ widthSize = config.widthSize; heightSize = config.heightSize; selecteIcon = config.selectIcon; unselectIcon = config.unselectIcon; totalScore = config.totalScore; currentScore = config.currentScore; selected = config.selected; } var selectedStyle = {width:widthSize,height:heightSize,position:"absolute"}; var unselectedStyle = {width:widthSize,height:heightSize}; this.state = { totalScore: totalScore, currentScore: currentScore, containerStyle:containerStyle!=undefined?containerStyle:styles.container, selectedStyle:selectedStyle, unselectedStyle:unselectedStyle, }; } render() { var containerStyle = this.state.containerStyle; var selectedStyle = this.state.selectedStyle; var unselectedStyle = this.state.unselectedStyle; this._getScore(callBack,this.state.currentScore); return ( <View style={containerStyle}> {this._addScoreIcon(selectedStyle,unselectedStyle)} </View> ); } _addScoreIcon(selectedStyle,unselectedStyle) { let images = []; for (var i = 1; i <= this.state.totalScore; i++) { let currentCount = i; images.push( <View key={"i" + i}> <TouchableWithoutFeedback onPress={(i) => {this._updateScore(currentCount)}}> <View> <Image source={{uri:unselectIcon}} style={unselectedStyle}/> {this._addSelectIcon(i,selectedStyle)} </View> </TouchableWithoutFeedback> </View> ); } return images; } _addSelectIcon(count,selectedStyle) { if (count <= this.state.currentScore) { return ( <Image source={{uri:selecteIcon}} style={selectedStyle}/> ); } } _updateScore(i) { if(selected){ this.setState({ currentScore: i }); } } _getScore(callback,currentScore){ return callback(currentScore); }}const styles = StyleSheet.create({ container:{ width:width, height:30, backgroundColor:"red", flexDirection: 'row', marginBottom: 6, }});
使用方式如下:
var config = { selected:true, totalScore:5, currentScore:2, selectIcon:"yellow_start", unselectIcon :"grey_start", widthSize:20, heightSize:20 }; return( <RattingView config ={config} callback = {(current)=>{console.log("----评分结果的回调--",current);}} ></RattingView> );
注意:config对象主要配置控件的图案,图案大小,总分数,当前分数,是否可选择。必传! callback是评分结果的回调,必传!
阅读全文
0 0
- ReactNative进阶之评分控件的封装
- ReactNative进阶之react-native-storage的使用及封装
- ReactNative系列之十四评分效果的实现
- 【Android 开发】:UI控件之 RatingBar 评分控件的使用
- 封装的评分星星类
- ReactNative自定义控件之 RefreshLayout
- IOS、Android进阶之ReactNative热更新
- 改写控件之《基于MaterialDialog实现的评分Dialog》
- 高级控件之星际评分条(RatingBar)的使用
- 评分星星的控件类
- 第四章、ReactNative组件的封装
- ReactNative系列之十对Http的Get和Post的简单封装
- Android 控件之RatingBar评分条(五星)
- Android 控件之十一:RatingBar评分条
- 评分控件
- 评分控件
- 评分控件
- ReactNative系列之十一原生RN控件WebView与Html5之件的调用
- python urllib2详解及实例
- MVC三层写法解剖
- 低价购买
- gmapping讲解
- Android dataBinding 使用记录
- ReactNative进阶之评分控件的封装
- TensorFlow下载安装&入门教程
- grep的-A-B-选项详解(转)
- 单一职责原则
- [bigdata-081] 用spring实现一个json返回值的api并部署到tomcat上
- Uva 673 平衡的括号——栈的操作
- Js判断元素是否在数组内
- HTML、JSP、Servlet中的相对路径和绝对路径 页面跳转问题
- 胖男孩