前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >为RecyclerView添加下拉刷新功能

为RecyclerView添加下拉刷新功能

作者头像
BennuCTech
发布2021-12-29 12:27:27
8230
发布2021-12-29 12:27:27
举报
文章被收录于专栏:BennuCTechBennuCTech

前言

在之前的文章中,我们实现了带有header和footer功能的WrapRecyclerView:实现一个带有header和footer功能的RecyclerView

现今App中列表的下拉刷新和上拉加载已经是一种习惯了,这两个操作也确实方便很多。

为RecyclerView添加这个功能可以通过多种方法,这里我选用了一种简单的做法。基于pulltorefresh这个库。

com.loopeer.android.thirdparty:pulltorefresh:<版本>

代码

首先要为WrapRecyclerView添加两个方法,如下:

代码语言:javascript
复制
    public int getFirstVisiblePosition(){
        int firstPosition = 0;
        LayoutManager layoutManager = getLayoutManager();
        if(layoutManager instanceof LinearLayoutManager){
            firstPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        }
        if(layoutManager instanceof GridLayoutManager){
            firstPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
        }
        if(layoutManager instanceof StaggeredGridLayoutManager){
            int[] positions = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(null);
            firstPosition = positions[0];
        }
        return firstPosition;
    }


    public int getLastVisiblePosition(){
        int lastPosition = 0;
        LayoutManager layoutManager = getLayoutManager();
        if(layoutManager instanceof LinearLayoutManager){
            lastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        }
        if(layoutManager instanceof GridLayoutManager){
            lastPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
        }
        if(layoutManager instanceof StaggeredGridLayoutManager){
            int[] positions = ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(null);
            lastPosition = positions[positions.length - 1];
        }
        return lastPosition;
    }

这两个方法用于辅助判断滑动时是否到顶或到底,下面会用到。注意对于不同的LayoutManager使用不同的方式来获取。

新建一个PullToRefreshRecyclerView,继承PullToRefreshBase

代码语言:javascript
复制
public class PullToRefreshRecyclerView extends PullToRefreshBase<WrapRecyclerView>{

需要重写几个方法来实现功能,如

代码语言:javascript
复制
    @Override
    protected boolean isReadyForPullEnd() {
        int lastPosition = getRefreshableView().getLastVisiblePosition();
        RecyclerView.LayoutManager layoutManager = getRefreshableView().getLayoutManager();
        View lastView = layoutManager.findViewByPosition(lastPosition);
        if(lastView != null) {
            int lastBottom = lastView.getBottom();
            return lastPosition == getRefreshableView().getRealItemCount() - 1 && lastBottom <= getRefreshableView().getBottom();
        }
        else{
            return true;
        }
    }

    @Override
    protected boolean isReadyForPullStart() {
        int firstPosition = getRefreshableView().getFirstVisiblePosition();
        RecyclerView.LayoutManager layoutManager = getRefreshableView().getLayoutManager();
        View firstView = layoutManager.findViewByPosition(firstPosition);
        if(firstView != null) {
            int firstTop = firstView.getTop();
            return firstPosition == 0 && firstTop >= 0;
        }
        else{
            return true;
        }
    }

这两个方法会在滑动的时候被调用,判断是否已经到列表顶部或底部,如果到顶部或底部就会执行下拉/上拉的操作了。

逻辑比较简单,判断是否显示了第一个/最后一个item,并且它的top/bottom也显示了(说明这个item完整显示出来了)。

还需要重写另外一个方法

代码语言:javascript
复制
    @Override
    protected WrapRecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        WrapRecyclerView recyclerView = new WrapRecyclerView(context, attrs);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(isReadyForPullStart()){
                    recyclerView.clearFocus();
                }
            }
        });
        recyclerView.setId(R.id.pulltorefresh_recyclerview);
        return recyclerView;
    }

这个方法就是创建一个WrapRecyclerView,注意不要忘了setId,否则在Fragment中使用会出现一些问题(回收重建的时候)。

效果

由于基于pulltorefresh库,所有功能库中都实现了,所以重写这几个方法就能实现下拉刷新功能了。实现效果如下

如果想改变显示或风格,可以通过pulltorefresh库的api来实现,关于pulltorefresh库的使用大家可以自行查阅相关文档。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-12-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BennuCTech 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 代码
  • 效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档