#
轉載請標明出處:
[http://blog.csdn.net/developer_jiangqq/article/details/50672747](http://blog.csdn.net/developer_jiangqq/article/details/50672747)
本文出自:[【江清清的博客】](http://blog.csdn.net/developer_jiangqq)
# (一)前言
?????????【好消息】個人網站已經上線運行,后面博客以及技術干貨等精彩文章會同步更新,請大家關注收藏:[http://www.lcode.org](http://www.lcode.org/)???????
? ? ? ?今天我們一起來看一下RefreshControl下拉刷新組件講解以及使用實例
??????????剛創建的React Native技術交流1群(282693535),React Native交流2群:(496601483),請不要重復加群!歡迎各位大牛,React?Native技術愛好者加入交流!同時博客左側歡迎微信掃描關注訂閱號,移動技術干貨,精彩文章技術推送!
??????????該組件和上一篇組將的PullToRefreshAndroidView組件相類似([點擊進入](http://www.lcode.org/%E3%80%90react-native%E5%BC%80%E5%8F%91%E3%80%91react-native%E6%8E%A7%E4%BB%B6%E4%B9%8Bpulltorefreshviewandroid%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0%E7%BB%84%E4%BB%B6%E8%AE%B2%E8%A7%A320/)),也是實現下拉刷新的功能。不過該組件是用在ScrollView的內部的,為ScrollView添加一個下拉刷新的功能。當ScrollView的垂直方向的偏移量scrollY:0的時候,手指往下拖拽ScrollView就會觸發onRefresh事件方法。
# (二)屬性方法
1. onRefresh? function方法?當視圖開始刷新的時候調用
2. refreshing? bool??決定加載進去指示器是否為活躍狀態,也表名當前是否在刷新中
3. colors [ColorPropType]?? android平臺適用??進行設置加載進去指示器的顏色,至少設置一種,最好可以設置4種
4. enabled? bool?? android平臺適用???用來設置下拉刷新功能是否可用
5. progressBackgroundColor ColorPropType??設置加載進度指示器的背景顏色
6. size?RefreshLayoutConsts.SIZE.DEFAULT? android平臺適用??加載進度指示器的尺寸大小?,具體可以查看RefreshControl.SIZE([詳細點擊進入](https://github.com/facebook/react-native/blob/master/Libraries/Components/RefreshControl/RefreshControl.js))
7. tintColor ColorPropType???iOS平臺適用??設置加載進度指示器的顏色
8. title string iOS平臺適用??設置加載進度指示器下面的標題文本信息
# (三)使用實例
?????????上面已經對于RefreshControl組件的基本介紹以及相關屬性做了說明,下面來進行實例使用一下,以下代碼在官方實例中進行修改而來,還是比較簡單的。具體代碼如下:
~~~
'use strict';
const React =require('react-native');
const {
AppRegistry,
ScrollView,
StyleSheet,
RefreshControl,
Text,
View,
} = React;
const styles =StyleSheet.create({
row: {
borderColor: 'red',
borderWidth: 5,
padding: 20,
backgroundColor: '#3a5795',
margin: 5,
},
text: {
alignSelf: 'center',
color: '#fff',
},
scrollview: {
flex: 1,
},
});
const Row =React.createClass({
_onClick: function() {
this.props.onClick(this.props.data);
},
render: function() {
return (
<View style={styles.row}>
<Text style={styles.text}>
{this.props.data.text}
</Text>
</View>
);
},
});
constRefreshControlDemo = React.createClass({
getInitialState() {
return {
isRefreshing: false,
loaded: 0,
rowData: Array.from(new Array(20)).map(
(val, i) => ({text:'初始行 ' + i})),
};
},
render() {
const rows = this.state.rowData.map((row,ii) => {
return <Row key={ii} data={row}/>;
});
return (
<ScrollView
style={styles.scrollview}
refreshControl={
<RefreshControl
refreshing={this.state.isRefreshing}
onRefresh={this._onRefresh}
colors={['#ff0000', '#00ff00','#0000ff','#3ad564']}
progressBackgroundColor="#ffffff"
/>
}>
{rows}
</ScrollView>
);
},
_onRefresh() {
this.setState({isRefreshing: true});
setTimeout(() => {
// 準備下拉刷新的5條數據
const rowData = Array.from(new Array(5))
.map((val, i) => ({
text: '刷新行 ' + (+this.state.loaded + i)
}))
.concat(this.state.rowData);
this.setState({
loaded: this.state.loaded + 5,
isRefreshing: false,
rowData: rowData,
});
}, 5000);
},
});
AppRegistry.registerComponent('RefreshControlDemo',() => RefreshControlDemo);
~~~
具體運行效果如下:

# (四)最后總結
??????????今天我們主要學習一下RefreshControl組件的基本介紹和實例演示使用,整體實現的功能還是和之前的PullToRefreshAndroidView一樣的。大家有問題可以加一下群React Native技術交流群(282693535)或者底下進行回復一下。?
? ? ? 尊重原創,轉載請注明:From Sky丶清([http://blog.csdn.net/developer_jiangqq](http://blog.csdn.net/developer_jiangqq)) 侵權必究!
? ? ? ?關注我的訂閱號(codedev123),每天分享移動開發技術(Android/IOS),項目管理以及博客文章!(歡迎關注,第一時間推送精彩文章)

? ? ?關注我的微博,可以獲得更多精彩內容
? ? ??[](http://weibo.com/u/1855428195?s=6uyXnP)
- 前言
- React Native For Android環境配置以及第一個實例(1)
- React Native開發IDE安裝及配置(2)
- React Native應用設備運行(Running)以及調試(Debugging)(3)
- React Native移植原生Android項目(4)
- React Native進行簽名打包成Apk(5)
- React Native庫版本升級(Upgrading)與降級講解(6)
- React Native控件之View視圖講解(7)
- React Native配置運行官方例子-初學者的福音(8)
- React Native控件之Text組件講解(9)
- React Native控件之Image組件講解與美團首頁頂部效果實例(10)
- TextInput組件講解與QQ登錄界面實現(11)
- ProgressBarAndroid進度條講解(12)
- DrawerLayoutAndroid抽屜導航切換組件講解(13)
- ScrollView組件講解(14)
- ToolbarAndroid工具欄控件講解以及使用(15)
- Switch開關與Picker選擇器組件講解以及使用(16)
- ViewPagerAndroid講解以及美團首頁頂部效果實例(17)
- Touchable*系列組件詳解(18)
- React Native專題文章講解,不斷更新中.....
- ListView組件講解以及最齊全實例(19)
- 超詳細Windows版本編譯運行React Native官方實例UIExplorer項目(多圖慎入)
- React Native超棒的LayoutAnimation(布局動畫)
- PullToRefreshViewAndroid下拉刷新組件講解(20)
- RefreshControl組件詳解(21)
- WebView組件詳解以及實例使用(22)