项目地址:https://github.com/moscoper/PullToRefresh.git
前言
一般的刷新和加载更多的效果都是竖直方向的(下拉刷新和上拉加载更多),本篇文章就利用 PullToReFresh 和 RecycleView 来实现水平方向的刷新和加载更多的效果。我们暂且叫它 PullToReshHorizontalRecycleView。
PullToReshHorizontalRecycleView
我们在 PullToRefresh 的扩展 这篇文章中介绍过如何利用 PullToReFresh 对其他可以滑动的控件添加下拉和上拉的效果。PullToReshHorizontalRecycleView 的实现和其类似。
继承 PullToRefreshBase
新建类 PullToReshHorizontalRecycleView
继承 PullToRefreshBase<RecyclerView>
。并实现 getPullToRefreshScrollDirection
,createRefreshableView
,isReadyForPullStart
和 isReadyForPullEnd
这四个方法。
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