欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

在android應用中怎么添加一個上拉刷新下拉加載功能

在android應用中怎么添加一個上拉刷新下拉加載功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站制作、成都網(wǎng)站建設、外貿營銷網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的資陽網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

下拉刷新

首先我們給出如下幾個參數(shù),后面要用:

  private NestedScrollingParentHelper helper = null;
  private boolean IsRefresh = true;
  private boolean IsLoad = true;
  //滑動的總距離
  private int totalY = 0;
  private LinearLayout headerLayout = null;
  private MyRecyclerView myRecyclerView = null;
  private LinearLayout footerLayout = null;

既然是刷新,我們的滾動肯定是在父view之前的。所以我們需要在onNestedPreScroll這個方法里面寫上我們所需要改動的x,y值。

我們需要用父view去攔截它。

我們需要判斷dy的值是否大于0,因為大于0是刷新操作,小于0是加載操作。然后我們需要判斷recyclerview是否是縱向的而不是橫向的。

public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
    if (IsRefresh) {
      if (dy > 0) {
        if (myRecyclerView.isOrientation(0)) {
          totalY += dy;
          if ((totalY / 2) <= 0) {
            scrollTo(0, totalY / 2);
            consumed[1] = dy;
          } else {
            scrollTo(0, 0);
            consumed[1] = 0;
          }
        }
        return;
      }
    }

上拉加載

上面我也說了onNestedPreScroll這個方法中判斷dy<0才是加載操作。所以綜上所述,代碼變成了這樣:

 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
    if (totalY < 0 && myRecyclerView.isOrientation(0) || totalY > 0 && myRecyclerView.isOrientation(1)) {
      isfling = true;
    }
    if (IsRefresh) {
      if (dy > 0) {
        if (myRecyclerView.isOrientation(0)) {
          totalY += dy;
          if ((totalY / 2) <= 0) {
            scrollTo(0, totalY / 2);
            consumed[1] = dy;
          } else {
            scrollTo(0, 0);
            consumed[1] = 0;
          }
        }
        return;
      }
    }
    if (IsLoad) {
      if (dy < 0) {
        if (myRecyclerView.isOrientation(1)) {
          totalY += dy;
          if ((totalY / 2) >= 0) {
            scrollTo(0, totalY / 2);
            consumed[1] = dy;
          } else {
            scrollTo(0, 0);
            consumed[1] = 0;
          }
        }
        return;
      }
    }
  }

最后我們需要在子view滑動結束后,實行如下操作:

 //子view滑動結束調用
  //dyUnconsumed < 0 向下滾
  //dyUnconsumed > 0 向上滾
  public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    if (dyUnconsumed != 0) {
      totalY += dyUnconsumed;
      scrollTo(0, totalY / 2);
    }
  }

其實最主要的兩個方法已經解決了,其他到沒什么了,這邊,我把nestedscrolling的8個接口的功能和自定義recyclerview放出來。已變大家參考。希望大家都能實現(xiàn)自己的刷新加載。告別swiperefreshlayout。

添加header和footer

這里我們參考listview自帶的addheaderview和addfooterview。代碼如下:

 public void addHeaderView(View headerView, int headerHeight) {
    this.headerLayout.removeAllViews();
    this.headerLayout.addView(headerView);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, headerHeight);
    layoutParams.topMargin = -headerHeight;
    this.headerLayout.setLayoutParams(layoutParams);
  }

  public void addFooterView(View footerView, int footerHeight) {
    this.footerLayout.removeAllViews();
    this.footerLayout.addView(footerView);
    this.footerLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, footerHeight));
  }

幾個接口的實現(xiàn)

 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
    return true;
  }

  public void onNestedScrollAccepted(View child, View target, int axes) {
    helper.onNestedScrollAccepted(child, target, axes);
  }

  //父view攔截子view的滾動
  public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
    if (totalY < 0 && myRecyclerView.isOrientation(0) || totalY > 0 && myRecyclerView.isOrientation(1)) {
      isfling = true;
    }
    if (IsRefresh) {
      if (dy > 0) {
        if (myRecyclerView.isOrientation(0)) {
          totalY += dy;
          if ((totalY / 2) <= 0) {
            scrollTo(0, totalY / 2);
            consumed[1] = dy;
          } else {
            scrollTo(0, 0);
            consumed[1] = 0;
          }
        }
        return;
      }
    }
    if (IsLoad) {
      if (dy < 0) {
        if (myRecyclerView.isOrientation(1)) {
          totalY += dy;
          if ((totalY / 2) >= 0) {
            scrollTo(0, totalY / 2);
            consumed[1] = dy;
          } else {
            scrollTo(0, 0);
            consumed[1] = 0;
          }
        }
        return;
      }
    }
  }

  //子view滑動結束調用
  //dyUnconsumed < 0 向下滾
  //dyUnconsumed > 0 向上滾
  public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    if (dyUnconsumed != 0) {
      totalY += dyUnconsumed;
      scrollTo(0, totalY / 2);
    }
  }

  public void onStopNestedScroll(View child) {
    helper.onStopNestedScroll(child);
    if (onTouchUpListener != null) {
      isfling = false;
      onTouchUpListener.touchUp();
    }
  }

  public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
    return isfling;
  }

  public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
    return isfling;
  }

  public int getNestedScrollAxes() {
    return helper.getNestedScrollAxes();
  }

自定義recyclerview

既然是自己寫的刷新加載框架,總不能還有自定義layout中在放個recyclerview。多麻煩,自定義一個,直接放在里面,然后分別放個header和footer 就沒必要每次有頁面用到刷新都要寫一個布局。3個布局解決整個項目的刷新和加載。話不多說,代碼如下:

  private class MyRecyclerView extends RecyclerView {
    private StaggeredGridLayoutManager staggeredGridLayoutManager = null;
    private LinearLayoutManager linearLayoutManager = null;
    private GridLayoutManager gridLayoutManager = null;
    private boolean isScrollLoad = false;
    private boolean isScrollRefresh = false;

    public MyRecyclerView(Context context) {
      super(context);
      setVerticalFadingEdgeEnabled(false);
      setHorizontalFadingEdgeEnabled(false);
      setVerticalScrollBarEnabled(false);
      setHorizontalScrollBarEnabled(false);
      setOverScrollMode(OVER_SCROLL_NEVER);
      setItemAnimator(new DefaultItemAnimator());
    }

    private void setMyLayoutManager(LayoutManager layoutManager) {
      if (layoutManager instanceof StaggeredGridLayoutManager) {
        staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
      } else if (layoutManager instanceof GridLayoutManager) {
        gridLayoutManager = (GridLayoutManager) layoutManager;
      } else if (layoutManager instanceof LinearLayoutManager) {
        linearLayoutManager = (LinearLayoutManager) layoutManager;
      }
      setLayoutManager(layoutManager);
      if (!isVertical()) {
        throw new NullPointerException("vertical!");
      }
    }

    private boolean isOrientation(int orientation) {//orientation,0代表向下,1代表向上
      if (orientation == 0)
        return isCanPullDown();
      else if (orientation == 1)
        return isCanPullUp();
      return false;
    }

    private boolean isCanPullDown() {
      return !canScrollVertically(-1);
    }

    private boolean isCanPullUp() {
      return !canScrollVertically(1);
    }

//    private int scrollLoad() {
//      int lastItem = 0;
//      int itemCount = 0;
//      int spanCount = 1;
//      if (staggeredGridLayoutManager != null) {
//        lastItem = staggeredGridLayoutManager.findLastVisibleItemPositions(null)[0];
//        itemCount = staggeredGridLayoutManager.getItemCount();
//        spanCount = staggeredGridLayoutManager.getSpanCount();
//      } else if (linearLayoutManager != null) {
//        lastItem = linearLayoutManager.findLastVisibleItemPosition();
//        itemCount = linearLayoutManager.getItemCount();
//        spanCount = 1;
//      } else if (gridLayoutManager != null) {
//        lastItem = gridLayoutManager.findLastVisibleItemPosition();
//        itemCount = gridLayoutManager.getItemCount();
//        spanCount = gridLayoutManager.getSpanCount();
//      }
//      return ((itemCount - 1) / spanCount + 1) - (lastItem / spanCount + 1);
//    }

    private boolean isVertical() {
      if (staggeredGridLayoutManager != null)
        return staggeredGridLayoutManager.getOrientation() == StaggeredGridLayoutManager.VERTICAL;
      else if (linearLayoutManager != null)
        return linearLayoutManager.getOrientation() == LinearLayoutManager.VERTICAL;
      else if (gridLayoutManager != null)
        return gridLayoutManager.getOrientation() == GridLayoutManager.VERTICAL;
      return false;
    }

//    public void onScrolled(int dx, int dy) {
//      if (dy > 0 && !isScrollLoad) {
//        if (oLior != null) {
//          onScrollListener.scrollLoad(sc````````
`
``
llLoad());//傳遞滾動到倒數(shù)第幾行
//        }
//      }
//    }
  }

關于在android應用中怎么添加一個上拉刷新下拉加載功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。

分享名稱:在android應用中怎么添加一個上拉刷新下拉加載功能
URL標題:http://www.aaarwkj.com/article44/ipdphe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、用戶體驗、軟件開發(fā)、Google、微信公眾號做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
国产传媒在线观看网站| 91一区二区三区在线| 裸体性做爰免费视频网站| 白白色最新福利视频二| 中文字幕一区精品日韩| 18以下的人禁止看的视频| 日本福利影院在线观看| 国产成人免费视频大全| 亚洲男人天堂中文字幕| 精品国产一区=区三区乱码| 伊人丁香六月日日操操| 欧美日韩免费爱爱视频| 日本一区二区三区视频| 日韩一二三四区免费观看| 中文字幕乱码人妻一区二| 久久成人影院免费观看| 亚洲欧美日韩一区91| 久久精品熟女亚洲av韩国| 日本免费的高清一区二区| 国产综合亚洲欧美日韩| 日本啪啪精品一区二区三区| 亚洲av日韩av高潮| 粉嫩av一区二区三区四区| 亚洲熟妇亚洲熟妇亚洲熟妇| 中文字幕av在线日韩| 日韩人妻中文字幕亚洲| 欧美三级在线完整版免费| 欧美亚洲精品一区在线观看| 国产麻豆三级在线观看| 97人妻精品一区二区三区六| 欧美精品熟妇乱黑人最大| 日韩黄色免费在线观看| 欧美日本午夜福利在线观看| 未满十八禁止观看免费| 国产区一区二区三在线播放| 麻豆专区一区二区三区| 色婷婷国产精品高潮呻吟| 99热免费精品在线观看| 91人妻一区二区三区久久| 精品一区二区三区女同| 就去吻色综合一二三四|