PullToRefreshLayout +RecyclerView 实现上拉加载下拉刷新

本文介绍了一种自定义的RecyclerView实现方式,该方式支持上下拉刷新功能,并详细展示了如何通过判断不同LayoutManager来确定是否可以进行上下拉刷新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 ,xml布局:

com.xinshangyun.app.ui.view.PullToRefreshLayout
    android:id="@+id/fudan_refresh_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/title_bar"
    android:divider="@android:color/transparent"
    android:dividerHeight="@dimen/dp_0"
    android:listSelector="@android:color/transparent">

    <include layout="@layout/refresh_head" />

    <com.fudan.app.activity.faxian.PullableRecyclerView
        android:id="@+id/fudan_content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        android:dividerHeight="@dimen/dp_0"
        android:listSelector="@android:color/transparent"
        android:scrollbars="none"/>

    <include layout="@layout/load_more" />
</com.xinshangyun.app.ui.view.PullToRefreshLayout>

2,自定义的RecycleView

public class PullableRecyclerView extends RecyclerView  implements Pullable {
    private int lastPosition;
    private boolean mCanPullDown = true;
    private boolean mCanPullUp = true;
    private boolean pd = true;

    public PullableRecyclerView(Context context) {
        super(context);
    }

    public PullableRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PullableRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    //判断是否可以下拉刷新
    public void setNo(boolean pd){
        this.pd = pd;
    }

    //是否可以下拉
    public void setCanPullDown(boolean canPullDown) {
        this.mCanPullDown = canPullDown;
    }

    public void setCanPullUp(boolean canPullUp) {
        this.mCanPullUp = canPullUp;
    }

    /**
     * 可以下拉
     *
     * @return
     */
    @Override
    public boolean canPullDown() {
        if(mCanPullDown) {
            if (getChildCount() == 0) {
                return true;
            } else if (getChildAt(0).getTop() >= 0) {
                if (getLayoutManager() instanceof LinearLayoutManager) {
                    int firstVisibleItem = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
                    if (firstVisibleItem == 0) {
                        return true;
                    }
                } else if (getLayoutManager() instanceof GridLayoutManager) {
                    int firstVisibleItem = ((GridLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
                    if (firstVisibleItem == 0) {
                        return true;
                    }
                }else if(getLayoutManager() instanceof StaggeredGridLayoutManager){
                    //因为StaggeredGridLayoutManager的特殊性可能导致最后显示的item存在多个,所以这里取到的是一个数组
                    //得到这个数组后再取到数组中position值最大的那个就是最后显示的position值了
//                    int[] lastPositionsDown = new int[((StaggeredGridLayoutManager) getLayoutManager()).getSpanCount()];
                    int lastPositionsDown[] = ((StaggeredGridLayoutManager)getLayoutManager()).findFirstVisibleItemPositions(null);
                    if(findMin(lastPositionsDown)==0){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 可以上拉
     *
     * @return
     */
    @Override
    public boolean canPullUp() {
        if(mCanPullUp) {
            if (getChildCount() == 0) {
                return true;
            } else {
                RecyclerView.LayoutManager layoutManager = getLayoutManager();
                if (layoutManager instanceof GridLayoutManager) {
                    //通过LayoutManager找到当前显示的最后的item的position
                    lastPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
                } else if (layoutManager instanceof LinearLayoutManager) {
                    lastPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                    //因为StaggeredGridLayoutManager的特殊性可能导致最后显示的item存在多个,所以这里取到的是一个数组
                    //得到这个数组后再取到数组中position值最大的那个就是最后显示的position值了
                    int[] lastPositions = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()];
                    ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(lastPositions);
                    lastPosition = findMax(lastPositions);
                }
                //时判断界面显示的最后item的position是否等于itemCount总数-1也就是最后一个item的position
                //如果相等则说明已经滑动到最后了 
                if (lastPosition == getLayoutManager().getItemCount()-1 &&  this.computeVerticalScrollExtent() + this.computeVerticalScrollOffset()
                        >= this.computeVerticalScrollRange()) {
                    return true;
                }
            }
        }
        return false;

    }

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

 

 

首先吐槽一下现在流行的刷新库,一个字大,包涵个人很多集成到项目中不需要的类,也很难找到很满意的效果,所以自己自己动手丰衣足食,撸一个。1.概述对所有基础控件(包括,嵌套滑动例如RecyclerView、NestedScrollView,普通的TextView、ListView、ScrollerView、LinearLayout等)提供下拉刷新上拉加载的支持,处理了横向滑动冲突(例如:顶部banner的情况) ,且实现无痕过度。gradle (改用bintray-release,2017-7-8 16:00上传,以下暂时不会生效)compile 'com.yan:pullrefreshlayout:1.1.2'2.说明支持所有基础控件 loading 出现效果默认(STATE_FOLLOW、STATE_PLACEHOLDER_FOLLOW、STATE_CENTER、STATE_PLACEHOLDER_CENTER、STATE_FOLLOW_CENTER、STATE_CENTER_FOLLOW)  //-控件设置-     refreshLayout.autoRefresh();// 自动刷新     refreshLayout.setOverScrollDampingRatio(0.2f);//  值越大overscroll越短 default 0.2     refreshLayout.setAdjustTwinkDuring(3);// 值越大overscroll越慢 default 3     refreshLayout.setScrollInterpolator(interpolator);// 设置scroller的插值器     refreshLayout.setLoadMoreEnable(true);// 上拉加载是否可用 default false     refreshLayout.setDuringAdjustValue(10f);// 动画执行时间调节,越大动画执行越慢 default 10f     // 刷新或加载完成后回复动画执行时间,为-1时,根据setDuringAdjustValue()方法实现 default 300     refreshLayout.setRefreshBackTime(300);     refreshLayout.setDragDampingRatio(0.6f);// 阻尼系数 default 0.6     refreshLayout.setPullFlowHeight(400);// 拖拽最大范围,为-1时拖拽范围不受限制 default -1     refreshLayout.setRefreshEnable(false);// 下拉刷新是否可用 default false     refreshLayout.setPullTwinkEnable(true);// 回弹是否可用 default true      refreshLayout.setAutoLoadingEnable(true);// 自动加载是否可用 default false          // headerView和footerView需实现PullRefreshLayout.OnPullListener接口调整状态     refreshLayout.setHeaderView(headerView);// 设置headerView     refreshLayout.setFooterView(footerView);// 设置footerView          /**     * 设置header或者footer的的出现方式,默认7种方式     * STATE_FOLLOW, STATE_PLACEHOLDER_FOLLOW, STATE_PLACEHOLDER_CENTER     * , STATE_CENTER, STATE_CENTER_FOLLOW, STATE_FOLLOW_CENTER     * ,STATE_PLACEHOLDER     */     refreshLayout.setRefreshShowGravity(RefreshShowHelper.STATE_CENTER,RefreshShowHelper.STATE_CENTER);     refreshLayout.setHeaderShowGravity(RefreshShowHelper.STATE_CENTER)// header出现动画     refreshLayout.setFooterShowGravity(RefreshShowHelper.STATE_CENTER)// footer出现动画     // PullRefreshLayout.OnPullListener         public interface OnPullListener {             // 刷新或加载过程中位置相刷新或加载触发位置的百分比,时刻调用             void onPullChange(float percent);             void onPullReset();// 数据重置调用             void onPullHoldTrigger();// 拖拽超过触发位置调用             void onPullHoldUnTrigger();// 拖拽回到触发位置之前调用             void onPullHolding(); // 正在刷新             void onPullFinish();// 刷新完成         }3.demo用到的库loading 动画 AVLoadingIndicatorView(https://github.com/81813780/AVLoadingIndicatorView)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值