PullToReFresh 实现 RecycleView 横向滑动的刷新和加载更多

本文介绍了如何使用PullToReFresh库结合RecycleView实现水平方向的刷新和加载更多效果。通过继承PullToRefreshBase,实现getPullToRefreshScrollDirection、createRefreshableView、isReadyForPullStart和isReadyForPullEnd方法,来达到所需功能。文章提供完整代码,并指出用法与原有PullToReFresh控件相同。
摘要由CSDN通过智能技术生成

项目地址:https://github.com/moscoper/PullToRefresh.git

前言

一般的刷新和加载更多的效果都是竖直方向的(下拉刷新和上拉加载更多),本篇文章就利用 PullToReFresh 和 RecycleView 来实现水平方向的刷新和加载更多的效果。我们暂且叫它 PullToReshHorizontalRecycleView。

PullToReshHorizontalRecycleView

我们在 PullToRefresh 的扩展 这篇文章中介绍过如何利用 PullToReFresh 对其他可以滑动的控件添加下拉和上拉的效果。PullToReshHorizontalRecycleView 的实现和其类似。

继承 PullToRefreshBase

新建类 PullToReshHorizontalRecycleView 继承 PullToRefreshBase<RecyclerView>。并实现 getPullToRefreshScrollDirectioncreateRefreshableView,isReadyForPullStartisReadyForPullEnd 这四个方法。

getPullToRefreshScrollDirection

方法 getPullToRefreshScrollDirection 的返回值决定了滑动的方向。返回 Orientation.HORIZONTAL 时为水平滑动;返回Orientation.VERTICAL时为竖直滑动。这里我们需要的是水平方向的滑动所以返回Orientation.HORIZONTAL

@Override public Orientation getPullToRefreshScrollDirection() {
    return Orientation.HORIZONTAL;
  }
createRefreshableView

方法createRefreshableView的返回值是要添加刷新效果的控件,这里我们需要为横向滑动的 RecycleView 添加刷新效果所以就返回横向滑动的 RecycleView。

@Override protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
    RecyclerView recyclerView = new RecyclerView(context,attrs);
    LinearLayoutManager mannagerTwo = new LinearLayoutManager(context);
    mannagerTwo.setOrientation(LinearLayoutManager.HORIZONTAL);

    recyclerView.setLayoutManager(mannagerTwo);

    return recyclerView;
  }
isReadyForPullStart

方法isReadyForPullStart返回true时表示可以开始刷新的效果了;返回false时表示还不满足可以触发刷新的效果。我们这里触发展示刷新效果的条件是 RecycleView 的第一个 item 的左边缘在屏幕左边缘的右侧。所以isReadyForPullStart的具体实现可以如下:

@Override protected boolean isReadyForPullStart() {
    return isFirstItemVisible();
  }
private boolean 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值