【React Native開發】React Native控件之RefreshControl組件詳解(21)

轉載請標明出處:

http://blog.csdn.net/developer_jiangqq/article/details/50672747

本文出自:【江清清的博客】

()前言

         【好消息】個人網站已經上線運行,後面博客以及技術乾貨等精彩文章會同步更新,請大家關注收藏:http://www.lcode.org       

       今天我們一起來看一下RefreshControl下拉刷新組件講解以及使用實例

         剛創建的React Native技術交流3羣(496508742),React Native技術交流4羣(458982758),請不要重複加羣!歡迎各位大牛,React Native技術愛好者加入交流!同時博客左側歡迎微信掃描關注訂閱號,移動技術乾貨,精彩文章技術推送!

          該組件和上一篇組將的PullToRefreshAndroidView組件相類似(點擊進入),也是實現下拉刷新的功能。不過該組件是用在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(詳細點擊進入)
  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) 侵權必究!

       關注我的訂閱號(codedev123),每天分享移動開發技術(Android/IOS),項目管理以及博客文章!(歡迎關注,第一時間推送精彩文章)

     關注我的微博,可以獲得更多精彩內容

      

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章