react native之ScrollView下拉刷新效果
更新時間:2021年09月09日 15:51:13 作者:創(chuàng)客未來
這篇文章主要為大家詳細(xì)介紹了react native之ScrollView下拉刷新效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了react native之ScrollView下拉刷新效果的具體代碼,供大家參考,具體內(nèi)容如下
ScrollView的refreshControl屬性用于下拉刷新,只能用于垂直視圖,即horizontal不能為true。
1.創(chuàng)建自定義CKRefresh.js刷新組件
import React,{Component} from 'react';
import {
View,
Text,
StyleSheet,
ScrollView,
RefreshControl,
Dimensions
} from 'react-native';
const screenW=Dimensions.get('window').width;
export default class CKRefresh extends Component{
constructor(){
super();
this.state={
rowDataArr:Array.from(new Array(30)).map((value,index)=>({
title:'初始化數(shù)據(jù)'+index
})),
//是否顯示loading
isRefreshing:false,
loaded:0
}
}
render(){
const rowsArr=this.state.rowDataArr.map((row,index)=>(<Row data={row} key={index}/>))
return(
<ScrollView
refreshControl={
<RefreshControl
refreshing={this.state.isRefreshing}
onRefresh={()=>this._onRefresh()}
colors={['red','green','blue']}
title="正在加載中..."
/>
}
>
{rowsArr}
</ScrollView>
)
}
_onRefresh(){
//1.顯示指示器
this.setState({
isRefreshing:true
});
//2.模擬加載數(shù)據(jù)
setTimeout(()=>{
let newDataArr=Array.from(new Array(5)).map((value,index)=>({
title:'我是拉下來的數(shù)據(jù)'+(this.state.loaded+index)
})).concat(this.state.rowDataArr);
//更新狀態(tài)機
this.setState({
rowDataArr:newDataArr,
isRefreshing:false,
loaded:this.state.loaded+5
});
},2000);
}
}
class Row extends Component{
static defaultProps={
data:{}
};
render(){
return(
<View style={{
width:screenW,
height:40,
borderBottomWidth:1,
borderBottomColor:'red',
justifyContent:'center'
}}>
<Text>{this.props.data.title}</Text>
</View>
)
}
}
const styles=StyleSheet.create({
})
2.在App.js中引用
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import CKRefresh from './components/CKRefresh';
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={styles.mainViewStyle}>
<CKRefresh/>
</SafeAreaView>
</>
);
};
const styles=StyleSheet.create({
mainViewStyle:{
flex:1,
backgroundColor:'#fff',
}
});
export default App;
3.結(jié)果如圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
采用React編寫小程序的Remax框架的編譯流程解析(推薦)
這篇文章主要介紹了采用React編寫小程序的Remax框架的編譯流程解析(推薦),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
react-three/postprocessing庫的參數(shù)中文含義使用解析
這篇文章主要介紹了react-three/postprocessing庫的參數(shù)中文含義使用總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
React中useTransition鉤子函數(shù)的使用詳解
React?18的推出標(biāo)志著React并發(fā)特性的正式到來,其中useTransition鉤子函數(shù)是一個重要的新增功能,下面我們就來學(xué)習(xí)一下useTransition鉤子函數(shù)的具體使用吧2024-02-02

