推荐项目:React Native下拉刷新利器 - react-native-pull-to-refresh
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import PTRView from 'react-native-pull-to-refresh';
const App = () => {
const onRefresh = () => {
console.log('Refreshing...');
// 模拟数据加载,2秒后停止刷新
setTimeout(() => {
console.log('Refresh completed.');
PTRView.stopRefresh();
}, 2000);
};
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<PTRView onRefresh={onRefresh}>
<ScrollView contentContainerStyle={styles.scrollViewContentContainer}>
{/* 这里是你的内容 */}
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Pull down to refresh</Text>
</View>
</ScrollView>
</PTRView>
</SafeAreaView>
</>
);
};
const styles = StyleSheet.create({
sectionContainer: {
justifyContent: 'center',
alignItems: 'center',
height: 200,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
},
scrollViewContentContainer: {
paddingTop: 20,
},
});
export default App;
这个例子展示了如何在React Native应用中使用react-native-pull-to-refresh
库来为一个ScrollView
组件添加下拉刷新功能。当用户下拉并释放时,会触发onRefresh
函数,并且模拟了数据加载的过程。数据加载完成后,通过调用PTRView.stopRefresh()
来停止刷新动画。
评论已关闭