react native之ScrollView下拉刷新效果
程序员文章站
2022-07-24 21:34:05
本文实例为大家分享了react native之scrollview下拉刷新效果的具体代码,供大家参考,具体内容如下scrollview的refreshcontrol属性用于下拉刷新,只能用于垂直视图,...
本文实例为大家分享了react native之scrollview下拉刷新效果的具体代码,供大家参考,具体内容如下
scrollview的refreshcontrol属性用于下拉刷新,只能用于垂直视图,即horizontal不能为true。
1.创建自定义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:'初始化数据'+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.模拟加载数据 settimeout(()=>{ let newdataarr=array.from(new array(5)).map((value,index)=>({ title:'我是拉下来的数据'+(this.state.loaded+index) })).concat(this.state.rowdataarr); //更新状态机 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.结果如图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 无惧SSD取代 东芝称HDD硬盘很重要:便宜、写不死
下一篇: React实现二级联动(左右联动)
推荐阅读
-
React native ListView 增加顶部下拉刷新和底下点击刷新示例
-
react-native ListView下拉刷新上拉加载实现代码
-
React Native之ListView实现九宫格效果的示例
-
react native之ScrollView下拉刷新效果
-
React Native 自定义下拉刷新上拉加载的列表的示例
-
react native基于FlatList下拉刷新上拉加载实现代码示例
-
React Native中的RefreshContorl下拉刷新使用
-
React Native中RefreshContorl下拉刷新教程
-
React Native_手把手教你做项目(五.下拉刷新RefreshControl&封装自定义Cell)
-
React native ListView 增加顶部下拉刷新和底下点击刷新示例