最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react native基于FlatList下拉刷新上拉加載實現(xiàn)代碼示例

 更新時間:2018年09月30日 10:13:32   作者:JsLin_  
這篇文章主要介紹了react native基于FlatList下拉刷新上拉加載實現(xiàn)代碼示例

react native 的上拉加載一直困擾著自己,一直用的第三方組件,但是可維護(hù)性不高,而且也不太好用,最近工作沒那么忙,就研究下了官方的FlatList,做出來的成果,比第三方組件流暢度高好多,而且也很好用

官方介紹:https://reactnative.cn/docs/flatlist/

下面是效果圖:

ios效果圖

android效果圖

總體思路就是:就是計算屏幕高度,然后減去導(dǎo)航的頭部,根據(jù)列表高度計算出每頁的個數(shù),然后向上取整。這樣做的目的是:防止不滿屏狀態(tài)下的,onEndReached函數(shù)的主動觸發(fā)。

方法實現(xiàn):

 //滿屏頁面判斷
 fullScreenJusting(ItemHeight) {
  const screnHeight = screnInfo.size.height;   //屏幕高度
  //計算列表個數(shù)
  const listNum = (screnHeight - 40) / ItemHeight;
  return Math.ceil(listNum);
 }

下拉刷新用的是 RefreshControl

官網(wǎng)地址:https://reactnative.cn/docs/refreshcontrol/#progressbackgroundcolor

具體代碼:

import React, { Component } from 'react';
import {
 View,
 Text,
 Image,
 StyleSheet,
 FlatList,
 RefreshControl,
 ActivityIndicator,
} from 'react-native';
import { SafeAreaView } from 'react-navigation';
import screnInfo from '../utils/View';
import BaseStyle from '../constants/Style';
import { QUESTION_LIST } from '../constants/Api';
import { form_req } from '../utils/Request';

export default class TestScreen extends Component {
 constructor(props) {
  super(props);
  this.state = {
   data: [
   ],
   refreshing: false,
   fresh: true,
   animating: true,
   nomore: false,
   pageSize: 0,
   pageNumber: 1,
  };
 }
 componentDidMount() { //初始化的時候要判斷長度 控制上拉加載

  const ListNums = this.fullScreenJusting(50);
  this.setState({
   pageSize: ListNums
  })
  this.onEndReachedCalled = false;
   this.getOrderList(ListNums, 1, true);

 }
 //滿屏頁面判斷
 fullScreenJusting(ItemHeight) {
  const screnHeight = screnInfo.size.height;   //屏幕高度
  //計算列表個數(shù)
  const listNum = (screnHeight - 40) / ItemHeight;
  return Math.ceil(listNum);
 }

 getOrderList(ListNums, pageNumber, fresh) {
  let nomore;
  form_req(QUESTION_LIST, {
   page: pageNumber,
   perpage: ListNums,
  }).then(res => {
   if (res.code == 200) {
    const item = res.data;
    if (item.length < ListNums) {
     nomore = true
    } else {
     nomore = false
    }
    if (fresh) {
     this.setState({
      data: item,
      nomore: nomore
     })
     
    } else {
     this.setState({
      data: this.state.data.concat(item),
      nomore: nomore,
     })
    }
    // this.onEndReachedCalledDuringMomentum = true;

   } else {

   }
  });
 }

 renderItem = item => {
  return (
   <View style={styles.item} key={item.id}>
    <Text>{item.name}</Text>
   </View>
  );
 };
 //列表線
 ItemSeparatorComponent = () => {
  return <View style={styles.baseLine} />;
 };
 //頭部
 ListHeaderComponent = () => { };
 //尾部
 ListFooterComponent = () => {
  return (
   <View style={styles.bottomfoot}>
    {
     this.state.data.length != 0 ?
      this.state.nomore ? (
       <Text style={styles.footText}>- 我是有底線的 -</Text>
      ) : (
        <View style={styles.activeLoad}>
         <ActivityIndicator size="small" animating={this.state.animating} />
         <Text style={[styles.footText, styles.ml]}>加載更多...</Text>
        </View>
       )
      :
      null
    }

   </View>
  );
 };
 //為空時
 ListEmptyComponent() {
  return (
   <View style={styles.noListView}>
    {/* <Image
     style={styles.noListImage}
     source={require('../images/status/order_no_record.png')}
    /> */}
    <Text style={styles.NoListText}>暫無訂單</Text>
   </View>
  );
 }
 _keyExtractor = (item,index) => item.id;

 _onEndReached = () => {
  if (!this.state.nomore && this.onEndReachedCalled ) {
   this.getOrderList(this.state.pageSize, ++this.state.pageNumber, false);
  }
  this.onEndReachedCalled=true;

 };
 _onRefresh() {
  this.setState({ nomore: false, pageNumber: 1 }, () => {
   this.getOrderList(this.state.pageSize, 1, true);
  })

 }

 render() {
  return (
   <SafeAreaView style={BaseStyle.flex}>
    <View style={styles.listConten}>
     <FlatList
      data={this.state.data}
      keyExtractor={this._keyExtractor}
      onEndReached={this._onEndReached}
      refreshing={true}
      renderItem={({ item }) => this.renderItem(item)}
      ItemSeparatorComponent={this.ItemSeparatorComponent}
      ListEmptyComponent={this.ListEmptyComponent}
      ListFooterComponent={this.ListFooterComponent}
      onEndReachedThreshold={0.1}
      refreshControl={
       <RefreshControl
        refreshing={this.state.refreshing}
        colors={['#ff0000', '#00ff00', '#0000ff']}
        progressBackgroundColor={"#ffffff"}
        onRefresh={() => {
         this._onRefresh();
        }}
       />
      }
     />
    </View>
   </SafeAreaView>
  );
 }
}

const styles = StyleSheet.create({
 listConten: {
  flex: 1,
  backgroundColor: '#ffffff',
 },
 item: {
  flexDirection: 'row',
  justifyContent: 'center',
  alignItems: "center",
  backgroundColor: '#ffffff',
  height: 50,
 },
 baseLine: {
  width: screnInfo.size.width,
  height: 1,
  backgroundColor: '#eeeeee',
 },
 noListView: {
  width: screnInfo.size.width,
  height: screnInfo.size.height - 140,
  justifyContent: 'center',
  alignItems: 'center',
 },
 NoListText: {
  marginTop: 15,
  fontSize: 18,
  color: '#999999',
 },
 noListImage: {
  width: 130,
  height: 140,
 },
 bottomfoot: {
  flexDirection: 'row',
  justifyContent: 'center',
  alignItems: 'center',
  padding: 10,
 },
 footText: {
  marginTop: 5,
  fontSize: 12,
  color: '#999999',
 },

 activeLoad: {
  flexDirection: 'row',
  justifyContent: 'center',
  alignItems: 'center',
 },
 ml: {
  marginLeft: 10,
 },
});

這里的坑就是:當(dāng)初始化進(jìn)來頁面的時候 上拉會主動觸發(fā),所以這里加了一個開關(guān) this.onEndReachedCalled = false; 初始化給一個false 當(dāng)觸發(fā)了 設(shè)為true,放在調(diào)取接口之后

代碼都很簡單易懂~ 有什么不懂的,或者有什么問題請留言,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React合成事件原理解析

    React合成事件原理解析

    事件是在編程時系統(tǒng)內(nèi)發(fā)生的動作或者發(fā)生的事情,而開發(fā)者可以某種方式對事件做出回應(yīng),而這里有幾個先決條件,這篇文章主要介紹了React合成事件原理解析,需要的朋友可以參考下
    2022-07-07
  • React使用公共文件夾public問題

    React使用公共文件夾public問題

    這篇文章主要介紹了React使用公共文件夾public問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • react實現(xiàn)同頁面三級跳轉(zhuǎn)路由布局

    react實現(xiàn)同頁面三級跳轉(zhuǎn)路由布局

    這篇文章主要為大家詳細(xì)介紹了react實現(xiàn)同頁面三級跳轉(zhuǎn)路由布局,一個路由小案例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • react中的watch監(jiān)視屬性-useEffect介紹

    react中的watch監(jiān)視屬性-useEffect介紹

    這篇文章主要介紹了react中的watch監(jiān)視屬性-useEffect使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • react vue背景掛載機(jī)器問題

    react vue背景掛載機(jī)器問題

    這篇文章主要介紹了react vue背景掛載機(jī)器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 詳解React中的useMemo和useCallback的區(qū)別

    詳解React中的useMemo和useCallback的區(qū)別

    React中的useMemo和useCallback是兩個重要的Hooks。常常被用于優(yōu)化組件的性能。雖然這兩個Hooks看起來很相似,但它們彼此之間還是有很大的區(qū)別的,隨著小編一起來學(xué)習(xí)吧
    2023-04-04
  • 詳解react setState

    詳解react setState

    這篇文章主要介紹了react setState的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用react,感興趣的朋友可以了解下
    2021-04-04
  • 詳細(xì)談?wù)凴eact中setState是一個宏任務(wù)還是微任務(wù)

    詳細(xì)談?wù)凴eact中setState是一個宏任務(wù)還是微任務(wù)

    學(xué)過react的人都知道,setState在react里是一個很重要的方法,使用它可以更新我們數(shù)據(jù)的狀態(tài),下面這篇文章主要給大家介紹了關(guān)于React中setState是一個宏任務(wù)還是微任務(wù)的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • 基于antd的autocomplete的二次封裝查詢示例

    基于antd的autocomplete的二次封裝查詢示例

    這篇文章主要為大家介紹了基于antd的autocomplete的二次封裝查詢示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • React 條件判斷實例詳解

    React 條件判斷實例詳解

    在 React 中,可以通過 JavaScript 的條件語句來動態(tài)渲染組件或元素,下面給大家分享幾種常用的在 React 中處理條件渲染的方法,感興趣的朋友跟隨小編一起看看吧
    2024-08-08

最新評論

新巴尔虎右旗| 永定县| 紫金县| 陕西省| 广东省| 云南省| 云浮市| 华安县| 南康市| 宜城市| 宁河县| 濮阳县| 琼海市| 保定市| 苏尼特右旗| 神池县| 麻江县| 逊克县| 敦煌市| 扬州市| 清丰县| 祁门县| 石嘴山市| 宜宾县| 白银市| 永定县| 晋江市| 新田县| 明光市| 阿鲁科尔沁旗| 平乡县| 固阳县| 榕江县| 平湖市| 绥德县| 商水县| 滦南县| 灌阳县| 巴彦县| 石首市| 永济市|