RefreshControl 下拉刷新
通过触发,立刻重新加载内容。规则
只能和 ListView 结合使用,不能单独使用。
可考虑定期自动刷新,eg:登录 app 后,自动刷新首页 List。
代码演示
下拉刷新
/* eslint no-dupe-keys: 0, no-mixed-operators: 0 */
import { RefreshControl, ListView } from 'antd-mobile';
const data = [
{
img: 'https://zos.alipayobjects.com/rmsportal/dKbkpPXKfvZzWCM.png',
title: '相约酒店',
des: '不是所有的兼职汪都需要风吹日晒',
},
{
img: 'https://zos.alipayobjects.com/rmsportal/XmwCzSeJiqpkuMB.png',
title: '麦当劳邀您过周末',
des: '不是所有的兼职汪都需要风吹日晒',
},
{
img: 'https://zos.alipayobjects.com/rmsportal/hfVtzEhPzTUewPm.png',
title: '食惠周',
des: '不是所有的兼职汪都需要风吹日晒',
},
];
let index = data.length - 1;
let pageIndex = 0;
const App = React.createClass({
getInitialState() {
const dataSource = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
});
this.initData = [];
for (let i = 0; i < 20; i++) {
this.initData.push(`r${i}`);
}
return {
dataSource: dataSource.cloneWithRows(this.initData),
refreshing: false,
};
},
onRefresh() {
this.setState({ refreshing: true });
setTimeout(() => {
this.initData = [`ref${pageIndex++}`, ...this.initData];
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.initData),
refreshing: false,
});
}, 1000);
},
onScroll() {
console.log('sss');
},
render() {
const separator = (sectionID, rowID) => (
<div
key={`${sectionID}-${rowID}`}
style={{
backgroundColor: '#F5F5F9',
height: 8,
borderTop: '1px solid #ECECED',
borderBottom: '1px solid #ECECED',
}}
/>
);
const row = (rowData, sectionID, rowID) => {
if (index < 0) {
index = data.length - 1;
}
const obj = data[index--];
return (
<div key={rowID}
style={{
padding: '0.08rem 0.16rem',
backgroundColor: 'white',
}}
>
<h3 style={{ padding: 2, marginBottom: '0.08rem', borderBottom: '1px solid #F6F6F6' }}>
{obj.title}
</h3>
<div style={{ display: '-webkit-box', display: 'flex' }}>
<img style={{ height: '1.28rem', marginRight: '0.08rem' }} src={obj.img} />
<div style={{ display: 'inline-block' }}>
<p>{obj.des}-{rowData}</p>
<p><span style={{ fontSize: '1.6em', color: '#FF6E27' }}>35</span>元/任务</p>
</div>
</div>
</div>
);
};
return (
<ListView
dataSource={this.state.dataSource}
renderRow={row}
renderSeparator={separator}
initialListSize={5}
pageSize={5}
scrollRenderAheadDistance={200}
scrollEventThrottle={20}
onScroll={this.onScroll}
style={{
height: document.body.clientHeight / 2,
border: '1px solid #ddd',
margin: '0.1rem 0',
}}
scrollerOptions={{ scrollbars: true }}
refreshControl={<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this.onRefresh}
/>}
/>
);
},
});
ReactDOM.render(<App />, mountNode);
API (web)
icon (any) - 刷新指示icon, 包含
pull
andrelease
状态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