PullToRefresh的RecyclerView封装

PullToRefresh就不用说了,非常的好用,原作者已经不更新了,所以不支持RecyclerView
不多说了,直接上代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class PullToRefreshRecyclerView extends PullToRefreshBase<RecyclerView>{
public PullToRefreshRecyclerView(Context context) {
super(context);
}

public PullToRefreshRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public PullToRefreshRecyclerView(Context context, Mode mode) {
super(context, mode);
}

public PullToRefreshRecyclerView(Context context, Mode mode, AnimationStyle style) {
super(context, mode, style);
}

@Override
public final Orientation getPullToRefreshScrollDirection() {
return Orientation.VERTICAL;
}

@Override
protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
return new RecyclerView(context, attrs);
}

@Override
protected boolean isReadyForPullStart() {
if (mRefreshableView.getChildCount() <= 0) {
return true;
}
int firstVisiblePosition = mRefreshableView.getChildPosition(mRefreshableView.getChildAt(0));
if (firstVisiblePosition == 0) {
return mRefreshableView.getChildAt(0).getTop() == mRefreshableView.getPaddingTop();
}
return false;
}

@Override
protected boolean isReadyForPullEnd() {
int lastItemPosition = mRefreshableView.getChildCount() - 1;
View lastItemView = mRefreshableView.getChildAt(lastItemPosition);
int lastVisiblePosition = mRefreshableView.getChildPosition(lastItemView);
// 是否是最后一个Item View
if (lastVisiblePosition >= mRefreshableView.getAdapter().getItemCount() - 1) {
return mRefreshableView.getChildAt(lastItemPosition).getBottom() <= mRefreshableView.getBottom();
}
return false;
}
}