RefreshControl 下拉刷新

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

规则

  • 只能和 ListView 结合使用,不能单独使用。

  • 可考虑定期自动刷新,eg:登录 app 后,自动刷新首页 List。

代码演示

ListView RefreshControl

下拉刷新

  1. /* eslint no-dupe-keys: 0, no-mixed-operators: 0 */
  2. import { RefreshControl, ListView } from 'antd-mobile';
  3. const data = [
  4. {
  5. img: 'https://zos.alipayobjects.com/rmsportal/dKbkpPXKfvZzWCM.png',
  6. title: '相约酒店',
  7. des: '不是所有的兼职汪都需要风吹日晒',
  8. },
  9. {
  10. img: 'https://zos.alipayobjects.com/rmsportal/XmwCzSeJiqpkuMB.png',
  11. title: '麦当劳邀您过周末',
  12. des: '不是所有的兼职汪都需要风吹日晒',
  13. },
  14. {
  15. img: 'https://zos.alipayobjects.com/rmsportal/hfVtzEhPzTUewPm.png',
  16. title: '食惠周',
  17. des: '不是所有的兼职汪都需要风吹日晒',
  18. },
  19. ];
  20. let index = data.length - 1;
  21. let pageIndex = 0;
  22. const App = React.createClass({
  23. getInitialState() {
  24. const dataSource = new ListView.DataSource({
  25. rowHasChanged: (row1, row2) => row1 !== row2,
  26. });
  27. this.initData = [];
  28. for (let i = 0; i < 20; i++) {
  29. this.initData.push(`r${i}`);
  30. }
  31. return {
  32. dataSource: dataSource.cloneWithRows(this.initData),
  33. refreshing: false,
  34. };
  35. },
  36. onRefresh() {
  37. this.setState({ refreshing: true });
  38. setTimeout(() => {
  39. this.initData = [`ref${pageIndex++}`, ...this.initData];
  40. this.setState({
  41. dataSource: this.state.dataSource.cloneWithRows(this.initData),
  42. refreshing: false,
  43. });
  44. }, 1000);
  45. },
  46. onScroll() {
  47. console.log('sss');
  48. },
  49. render() {
  50. const separator = (sectionID, rowID) => (
  51. <div
  52. key={`${sectionID}-${rowID}`}
  53. style={{
  54. backgroundColor: '#F5F5F9',
  55. height: 8,
  56. borderTop: '1px solid #ECECED',
  57. borderBottom: '1px solid #ECECED',
  58. }}
  59. />
  60. );
  61. const row = (rowData, sectionID, rowID) => {
  62. if (index < 0) {
  63. index = data.length - 1;
  64. }
  65. const obj = data[index--];
  66. return (
  67. <div key={rowID}
  68. style={{
  69. padding: '0.08rem 0.16rem',
  70. backgroundColor: 'white',
  71. }}
  72. >
  73. <h3 style={{ padding: 2, marginBottom: '0.08rem', borderBottom: '1px solid #F6F6F6' }}>
  74. {obj.title}
  75. </h3>
  76. <div style={{ display: '-webkit-box', display: 'flex' }}>
  77. <img style={{ height: '1.28rem', marginRight: '0.08rem' }} src={obj.img} />
  78. <div style={{ display: 'inline-block' }}>
  79. <p>{obj.des}-{rowData}</p>
  80. <p><span style={{ fontSize: '1.6em', color: '#FF6E27' }}>35</span>元/任务</p>
  81. </div>
  82. </div>
  83. </div>
  84. );
  85. };
  86. return (
  87. <ListView
  88. dataSource={this.state.dataSource}
  89. renderRow={row}
  90. renderSeparator={separator}
  91. initialListSize={5}
  92. pageSize={5}
  93. scrollRenderAheadDistance={200}
  94. scrollEventThrottle={20}
  95. onScroll={this.onScroll}
  96. style={{
  97. height: document.body.clientHeight / 2,
  98. border: '1px solid #ddd',
  99. margin: '0.1rem 0',
  100. }}
  101. scrollerOptions={{ scrollbars: true }}
  102. refreshControl={<RefreshControl
  103. refreshing={this.state.refreshing}
  104. onRefresh={this.onRefresh}
  105. />}
  106. />
  107. );
  108. },
  109. });
  110. ReactDOM.render(<App />, mountNode);

RefreshControl下拉刷新 - 图1

API (web)

  • icon (any) - 刷新指示icon, 包含 pull and release 状态

  • loading (any) - 加载指示器

  • distanceToRefresh (number, default: 50 / 2 * (window.devicePixelRatio || 2)) - 刷新距离

  • onRefresh (function, required) - 刷新回调函数

  • refreshing (boolean, false) - 是否显示刷新状态

icon/loading API 如何自己设置,参考这里 https://github.com/ant-design/ant-design-mobile/blob/master/components/refresh-control/index.web.tsx#L11

API (react-native)

见此:https://facebook.github.io/react-native/docs/refreshcontrol.html#props