RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
怎么在android中利RecycleView添加一个下滑功能

怎么在android中利RecycleView添加一个下滑功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联公司专注于企业营销型网站建设、网站重做改版、东乡网站定制设计、自适应品牌网站建设、HTML5商城建设、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为东乡等各大城市提供网站开发制作服务。

RecycleView内部有一个滑动监听的抽象类OnScrollListener来接收滚动事件,此类里面有两个实现的方法

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.
     * 

     * 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 当recyclerview的滚动状态发生变化的时候调用。

onScrolled 在布局可见和recycleview滚动的时候调用。

那么思路就是:

(1)在onScrollStateChanged 方法中判断当前的滚动状态是停止滚动的状态。

(2)然后根据api中的方法获得最后可见的位置。

(3)判断当前可见的recycleview中item的条数大于0

(4)判断最后可见的位置大于数大于item总数减一

(5)并且item的总数大于可见的item 这样可以保证超过一个界面的时候才执行。

当满足让面的要求的时候我们就可以通过接口回调执行我们的耗时逻辑 ,并显示出加载的dialog。

因为RecyclerView可以通过layoutManager灵活的转换成列表,表格,和瀑布流。尤其是瀑布流的时候,它的最后可见的位置是不一样的,所以我们必须根据其不同的layoutManager状态获取相对应的最后可见位置。

代码:

 @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;
  }

关于怎么在android中利RecycleView添加一个下滑功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


标题名称:怎么在android中利RecycleView添加一个下滑功能
分享地址:http://scyingshan.cn/article/pjsdpi.html