欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android中RecycleView添加下滑到底部的監(jiān)聽示例

 更新時(shí)間:2017年03月25日 17:11:30   作者:mingyunxiaohai  
本篇文章主要介紹了android中RecycleView添加下滑到底部的監(jiān)聽示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

我們的日常開發(fā)中經(jīng)常用到下拉刷新,而網(wǎng)上評(píng)價(jià)最好的開源下拉刷新組件當(dāng)然還是android-Ultra-Pull-To-Refresh 此組件可以給任何的控件添加下拉刷新功能。當(dāng)然也包括recycleview了。

可惜android-Ultra-Pull-To-Refresh只是提供了下拉刷新的功能,但是對(duì)于列表類的組件,我們?nèi)粘i_發(fā)中更多的會(huì)用到其上拉加載或者滑到底部自動(dòng)加載的功能,當(dāng)然目前來看用戶更喜歡滑到底部自動(dòng)加載的功能。就比如今天說的recycleview我們只能自己給其添加滑到底部加載更多的功能了。

那它的實(shí)現(xiàn)原理是神馬呢 非常簡(jiǎn)單:

RecycleView內(nèi)部有一個(gè)滑動(dòng)監(jiān)聽的抽象類OnScrollListener來接收滾動(dòng)事件,此類里面有兩個(gè)實(shí)現(xiàn)的方法

public abstract static class OnScrollListener {
    /**
     * Callback method to be invoked when RecyclerView's scroll state changes.
     *
     * @param recyclerView The RecyclerView whose scroll state has changed.
     * @param newState   The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
     *           {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
     */
    public void onScrollStateChanged(RecyclerView recyclerView, int newState){}

    /**
     * Callback method to be invoked when the RecyclerView has been scrolled. This will be
     * called after the scroll has completed.
     * <p>
     * This callback will also be called if visible item range changes after a layout
     * calculation. In that case, dx and dy will be 0.
     *
     * @param recyclerView The RecyclerView which scrolled.
     * @param dx The amount of horizontal scroll.
     * @param dy The amount of vertical scroll.
     */
    public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
  }

通多源碼的注釋可以了解到

onScrollStateChanged 當(dāng)recyclerview的滾動(dòng)狀態(tài)發(fā)生變化的時(shí)候調(diào)用。

onScrolled 在布局可見和recycleview滾動(dòng)的時(shí)候調(diào)用。

那么思路就是:

(1)在onScrollStateChanged 方法中判斷當(dāng)前的滾動(dòng)狀態(tài)是停止?jié)L動(dòng)的狀態(tài)。

(2)然后根據(jù)api中的方法獲得最后可見的位置。

(3)判斷當(dāng)前可見的recycleview中item的條數(shù)大于0

(4)判斷最后可見的位置大于數(shù)大于item總數(shù)減一

(5)并且item的總數(shù)大于可見的item 這樣可以保證超過一個(gè)界面的時(shí)候才執(zhí)行。

當(dāng)滿足讓面的要求的時(shí)候我們就可以通過接口回調(diào)執(zhí)行我們的耗時(shí)邏輯 ,并顯示出加載的dialog。

因?yàn)镽ecyclerView可以通過layoutManager靈活的轉(zhuǎn)換成列表,表格,和瀑布流。尤其是瀑布流的時(shí)候,它的最后可見的位置是不一樣的,所以我們必須根據(jù)其不同的layoutManager狀態(tài)獲取相對(duì)應(yīng)的最后可見位置。

代碼:

 @Override
  public void onScrollStateChanged(int state) {
    if (state == RecyclerView.SCROLL_STATE_IDLE && mLoadingListener != null) {
      LayoutManager layoutManager = getLayoutManager();
      int lastVisibleItemPosition;
      if (layoutManager instanceof GridLayoutManager) {
        lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
      } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int[] into = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
        ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(into);
        lastVisibleItemPosition = findMax(into);
      } else {
        lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
      }
      if (layoutManager.getChildCount() > 0
          && lastVisibleItemPosition >= layoutManager.getItemCount() - 1 && layoutManager.getItemCount() > layoutManager.getChildCount()) {
          View footView = mFootViews.get(0);
        footView.setVisibility(View.VISIBLE);
        mLoadingListener.onLoadMore();
      }
    }
  }

我們可以通過api獲取瀑布流的所有的列 ,通過下面的方法找出最下面的一列。將加載的dialog顯示在此列的下面。

 private int findMax(int[] lastPositions) {
    int max = lastPositions[0];
    for (int value : lastPositions) {
      if (value > max) {
        max = value;
      }
    }
    return max;
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論