PullToRefresh拉动刷新

通过触发,立刻重新加载内容。

规则

  • 可以和 ListView 结合使用,也可以单独使用。
  • 可考虑定期自动刷新, e.g. 登录 APP 后,自动刷新首页 List。

代码演示

拉动刷新

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'demo-pull-to-refresh-basic',
  4. template: `
  5. <NoticeBar *ngIf="!isMobile"
  6. style="margin-bottom: 10px"
  7. [option]="{'content':'该组件只支持Touch事件,请使用移动模式/设备打开此页。', 'marqueeProps': {fps: 100}}"
  8. ></NoticeBar>
  9. <div Button (onClick)="onClick()">direction: {{state.directionName}}</div>
  10. <PullToRefresh [ngStyle]="dtPullToRefreshStyle"
  11. [direction]="state.direction"
  12. [(ngModel)]="state.refreshState"
  13. [endReachedRefresh]="state.endReachedRefresh"
  14. (onRefresh)="pullToRefresh($event)"
  15. >
  16. <div *ngFor="let i of this.state.data" style="text-align: center; padding: 20px">{{i}}</div>
  17. </PullToRefresh>
  18. <ng-template #loading>
  19. <Icon type="loading"></Icon>
  20. </ng-template>
  21. `
  22. })
  23. export class DemoPullToRefreshBasicComponent implements OnInit {
  24. isMobile = /Android|webOS|iPhone|iPod|BlackBerry/i.test(window.navigator.userAgent);
  25. pageLimit = 20;
  26. public directionCount = 0;
  27. page = 0;
  28. state = {
  29. refreshState: {
  30. currentState: 'deactivate',
  31. drag: false
  32. },
  33. direction: '',
  34. endReachedRefresh: false,
  35. height: 500,
  36. data: [],
  37. directionName: 'both up and down'
  38. };
  39. dtPullToRefreshStyle = { height: this.state.height + 'px' };
  40. constructor() {}
  41. onClick() {
  42. this.directionCount ++;
  43. switch (this.directionCount) {
  44. case 0:
  45. this.state.direction = '';
  46. this.state.directionName = 'both up and down';
  47. break;
  48. case 1:
  49. this.state.direction = 'down';
  50. this.state.endReachedRefresh = true;
  51. this.state.directionName = 'down';
  52. break;
  53. case 2:
  54. this.state.direction = 'up';
  55. this.state.directionName = 'up';
  56. break;
  57. default:
  58. this.directionCount = 0;
  59. this.state.direction = '';
  60. this.state.directionName = 'both up and down';
  61. break;
  62. }
  63. }
  64. pullToRefresh(event) {
  65. if (event === 'endReachedRefresh') {
  66. if (this.page < 9) {
  67. this.page++;
  68. this.addItems(this.page * this.pageLimit);
  69. this.state.refreshState.currentState = 'release';
  70. setTimeout(() => {
  71. this.state.refreshState.currentState = 'finish';
  72. }, 1000);
  73. }
  74. } else {
  75. if (event === 'down') {
  76. this.state.data = [];
  77. this.page = 0;
  78. this.addItems(0);
  79. } else {
  80. if (this.page < 9) {
  81. this.page++;
  82. this.addItems(this.page * this.pageLimit);
  83. }
  84. }
  85. }
  86. }
  87. addItems(startIndex) {
  88. for (let i = startIndex; i < this.pageLimit * (this.page + 1); i++) {
  89. this.state.data.push(i);
  90. }
  91. }
  92. genData() {
  93. const dataArr = [];
  94. for (let i = 0; i < 100; i++) {
  95. dataArr.push(i);
  96. }
  97. return dataArr;
  98. }
  99. ngOnInit() {
  100. this.addItems(0);
  101. }
  102. }

PullToRefresh 拉动刷新 - 图1

API

属性说明类型默认值
direction拉动方向,可以是 updownString-
distanceToRefresh刷新距离number25
refreshing是否显示刷新状态boolfalse
onRefresh必选, 刷新回调函数() => void-
ngModel刷新的状态 { currentState : deactivate , drag: false}Objectdeactivate
headerIndicator头部指示器配置 { activate: any, deactivate: any, release: any, finish: any }Object-
footerIndicator脚部指示器配置 { activate: any, deactivate: any, release: any, finish: any }Object-
endReachedRefresh滚动到底自动刷新(direction=down)boolfalse

注: Need to set the height to use this component, otherwise it will not display correctly.