原生態(tài)刷新控件
在開發(fā)過程中尺铣,經(jīng)常要用到刷新控件描验,iOS
中比較熱門使用第三方的刷新控件占哟,然而卻忽略了蘋果系統(tǒng)原生的刷新效果华烟,整體簡潔大方美觀翩迈,同樣的在react-native
中也提供了這樣的刷新效果。以下將提供在ios
和rn
開發(fā)過程對RefreshControl
的編寫方法盔夜。
一负饲、IOS中使用方法
-(void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64,self.view.frame.size.width,self.view.frame.size.height-64) style:UITableViewStylePlain];
[self.viewaddSubview:_tableView];
_tableView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bj"]];
//刷新的控件
_control = [[UIRefreshControl alloc]init];
[_tableView addSubview:_control];
[_control addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
}
//刷新時調(diào)用的方法
-(void)refresh
{
if(_isLoading)
{
[_control endRefreshing];
return;
}
//模擬下載數(shù)據(jù)
_isLoading = YES;
NSData*data = [NSData dataWithContentsOfURL:[NSURLURLWithString:@"http://www.baidu.com"]];
NSLog(@"%ld",data.length);
[_tableView reloadData];
[_control endRefreshing];
_isLoading = NO;
}
二、RN中使用方法
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableOpacity,
Image,
ListView,
ScrollView,
RefreshControl
} from 'react-native';
// 導(dǎo)入外部組件
var Service = require('../../Common/Service');
var Util = require('../../Common/Util');
var Dimensions = require('Dimensions');
var {width, height} = Dimensions.get('window');
var ConsultList = React.createClass({
getDefaultProps(){
return{
popToHome:null,
classid:'',
htmlContent:null
}
},
getInitialState(){
//創(chuàng)建數(shù)據(jù)源
var ds = new ListView.DataSource({rowHasChanged:(row1, row2) => row1 !== row2});
//初始化數(shù)據(jù)源
return {
dataSource: ds,
isEmptyData:false,
isRefreshing:true
}
},
render() {
return (
this.state.isEmptyData ?
this.isEmptyData() :
<View style={styles.container}>
{ /*列表*/ }
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow}
refreshControl={
<RefreshControl
refreshing={this.state.isRefreshing}
tintColor="gray"
title="正在刷新"
onRefresh={this._onRefresh}/>}
/>
</View>
);
},
_onRefresh: function() {
console.log("刷新");
this.setState({isRefreshing: true});
this.getEduData();
},
popToHome(mark){
if (mark == null) return;
this.props.popToHome(mark);
},
popHeader(html){
if (html == null) return;
this.props.htmlContent(html);
},
renderRow(rowData){
console.log('rowData',rowData);
return(
<TouchableOpacity onPress={()=>this.popHeader(rowData.newscontent)}>
<View style={styles.listViewStyle}>
{ /*左邊*/ }
<Image source={{uri:rowData.newspic}} style={styles.imageViewStyle}/>
{ /*右邊*/ }
<View style={styles.rightViewStyle}>
<View style={styles.rightTopViewStyle}>
<Text style={{color:'#333333'}}>{rowData.newstitle}</Text>
</View>
<View style={styles.rightBottomViewStyle}>
<Text style={{color:'gray'}}>{rowData.newsremark}</Text>
</View>
</View>
</View>
</TouchableOpacity>
)
},
componentDidMount(){
//從網(wǎng)絡(luò)上請求數(shù)據(jù)
this.getEduData();
},
getEduData(){
var url = Service.consultListUrl;
let formData = new FormData();
formData.append("extend",'zhSPe69X8gPAI9Zk5tHMGc01aMyvch2dEmvzOWIr3pI=');
fetch(url,{
method:'POST',
headers:{
'Content-Type':'multipart/form-data',
},
body:formData,
})
.then((response) => response.text() )
.then((responseData)=>{
console.log('responseData',responseData);
this.getEduListData();
})
.catch((error)=>{console.error('error',error)
alert(error);
});
},
getEduListData(){
var url = Service.consultListUrl;
let formData = new FormData();
formData.append('extend','zhSPe69X8gPAI9Zk5tHMGVMFO4j2515k1lTzzhs+RC4=');
formData.append('classid',this.props.classid);
formData.append('keyValue','');
formData.append('page','1');
formData.append('pageSize','10');
fetch(url,{
method:'POST',
headers:{
'Content-Type':'multipart/form-data',
},
body:formData,
})
.then((response) => response.text() )
.then((responseData)=>{
// console.log('responseData',responseData);
// json格式化 JSON.stringify(responseData)轉(zhuǎn)字符串
console.log('json格式化',JSON.parse(responseData));
// alert(responseData);
if (!JSON.parse(responseData).data || JSON.parse(responseData).data.length==0) {
this.setState({
isEmptyData:true
});
}
//更新數(shù)據(jù)源
this.setState({
isRefreshing:false,
dataSource:this.state.dataSource.cloneWithRows(JSON.parse(responseData).data)
});
})
.catch((error)=>{console.error('error',error)
alert(error);
});
},
isEmptyData(){
return (
<ScrollView style={{backgroundColor:'#e8e8e8'}}>
<View style={styles.emptyDataStyle}>
<Image source={{uri:'bv_dropbox'}} style={styles.emptyDataIcon}/>
<Text style={{marginTop:5,color:'gray'}}>暫未有資訊</Text>
</View>
</ScrollView>
)
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#e8e8e8',
},
imageViewStyle: {
width:80,
height:100
},
listViewStyle: {
backgroundColor: 'white',
padding: 10,
borderBottomColor:'#e8e8e8',
borderBottomWidth:0.5,
flexDirection:'row'
},
rightViewStyle: {
marginLeft:8,
width:width-110,
justifyContent:'center'
},
rightTopViewStyle: {
flexDirection:'row',
marginBottom:7,
justifyContent:'space-between'
},
rightBottomViewStyle: {
flexDirection:'row',
marginTop:7,
justifyContent:'space-between'
},
emptyDataStyle:{
justifyContent:'center',
alignItems:'center',
marginTop:100
},
emptyDataIcon:{
width:50,
height:50,
}
});
module.exports = ConsultList;