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

PullToRefresh拉动刷新 - 图1

API

参数说明类型默认值
[direction]拉动方向‘up’ | ‘down’-
[distanceToRefresh]刷新距离number25
[refreshing]是否显示刷新状态booleanfalse
[headerIndicator]头部指示器配置 { activate: any, deactivate: any, release: any, finish: any }object-
[footerIndicator]脚部指示器配置 { activate: any, deactivate: any, release: any, finish: any }object-
[endReachedRefresh]滚动到底自动刷新(direction=down)booleanfalse
[(ngModel)]刷新的状态 { currentState : deactivate , drag: false}object{ currentState : deactivate , drag: false}
(onRefresh)刷新回调函数EventEmitter<void>-

注: 使用时,需要设置组件高度,否则展示不正确。