ReactNative进阶(十七):RefreshControl组件实现刷新效果

梦里梦外; 2023-01-19 10:58 86阅读 0赞

文章目录

    • 一、简介
    • 二、API
    • 三、应用示例

一、简介

刷新功能在数据更新的时候很常用,它对用户有一个非常明显的数据正在更新的提示信息。ReactNative中提供了RefreshControl组件来实现这个刷新功能。

RefreshControl组件可以用在ScrollViewListView内部,为其添加下拉刷新的功能。当ScrollView处于竖直方向的起点位置(scrollY: 0),此时下拉会触发一个onRefresh事件。

二、API

它是跨平台的组件,提供的属性如下:

//视图下拉开始刷新时调用。

  • onRefresh: React.PropTypes.func

//该视图是否应指示活动刷新。

  • refreshing: React.PropTypes.bool.isRequired

//刷新指示器的颜色。@platform ios

  • tintColor: ColorPropType

//刷新文案的颜色。@platform ios

  • titleColor: ColorPropType

//标题显示在刷新指示器下方。@platform ios

  • title: React.PropTypes.string

//是否启用拉动刷新功能。@platform android

  • enabled: React.PropTypes.bool

//用于绘制刷新指示器的颜色(至少一种)。@platform android colors:

  • React.PropTypes.arrayOf(ColorPropType)

//刷新指示器的背景色。@platform android

  • progressBackgroundColor: ColorPropType

//刷新指示器的大小,请参见RefreshControl.SIZE。@platform android size:

  • React.PropTypes.oneOf([RefreshLayoutConsts.SIZE.DEFAULT,RefreshLayoutConsts.SIZE.LARGE])

//进度视图顶部偏移。@platform android

  • progressViewOffset:React.PropTypes.number

三、应用示例

了解了api,简单示例如下:

ScrollRefreshControl.js

  1. import React, { Component } from 'react';
  2. import {
  3. StyleSheet,
  4. ScrollView,
  5. Text,
  6. RefreshControl
  7. } from 'react-native';
  8. export default class ScrollRefreshControl extends Component{
  9. //state数据
  10. state = { text: '初始状态', refreshing: false };
  11. //下拉视图开始刷新时调用
  12. _onRefresh() {
  13. if (this.state.refreshing === false) {
  14. this._updateState('正在刷新......', true);
  15. //5秒后结束刷新
  16. setTimeout( ()=>{
  17. this._updateState('结束状态', false)
  18. }, 5000)
  19. }
  20. }
  21. //更新State
  22. _updateState(message, refresh){
  23. this.setState({ text:message,refreshing: refresh});
  24. }
  25. render(){
  26. return (
  27. <ScrollView
  28. style={ [styles.flex,styles.bgColor]}
  29. contentContainerStyle={ { flex: 1, alignItems: 'center',justifyContent: 'center'}}
  30. indicatorStyle={ 'black'}
  31. showsHorizontalScrollIndicator={ true}
  32. bounces={ true}
  33. refreshControl={
  34. <RefreshControl
  35. tintColor={ 'red'}
  36. titleColor={ 'brown'}
  37. title={ '正在刷新......'}
  38. refreshing={ this.state.refreshing}
  39. onRefresh={ this._onRefresh.bind(this)}
  40. />
  41. }
  42. >
  43. <Text>{ this.state.text}</Text>
  44. </ScrollView>
  45. )
  46. }
  47. }
  48. const styles = StyleSheet.create({
  49. flex: {
  50. flex: 1
  51. },
  52. bgColor: {
  53. backgroundColor:'#EEE'
  54. }
  55. });

index.ios.js/index.android.js

  1. /** * Sample React Native App * https://github.com/facebook/react-native * @flow */
  2. import React, { Component } from 'react';
  3. import {
  4. AppRegistry,
  5. StyleSheet,
  6. View
  7. } from 'react-native';
  8. import ScrollRefreshControl from "./src/ScrollRefreshControl";
  9. export default class ReactNativeDemo extends Component {
  10. render() {
  11. return (
  12. <View style={ [styles.flex,styles.bgColor]}>
  13. <ScrollRefreshControl/>
  14. </View>
  15. );
  16. }
  17. }
  18. const styles = StyleSheet.create({
  19. flex: {
  20. flex: 1
  21. },
  22. bgColor: {
  23. backgroundColor: 'white'
  24. }
  25. });
  26. AppRegistry.registerComponent('ReactNativeDemo', () => ReactNativeDemo);

发表评论

表情:
评论列表 (有 0 条评论,86人围观)

还没有评论,来说两句吧...

相关阅读