PtrFrameLayout实现下拉刷新

Ultra Pull To Refresh:https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh

Gradle依赖

1
2
//下拉刷新
compile 'in.srain.cube:ultra-ptr:1.0.11'

自定义PtrFrameLayout

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class MyPullRefreshFrameLayout extends PtrFrameLayout {
public MyPullRefreshFrameLayout(Context context) {
super(context);
initViews();
}

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

public MyPullRefreshFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initViews();
}

private void initViews() {
ThreeDotScaleHeader header = new ThreeDotScaleHeader(getContext());
setHeaderView(header);
addPtrUIHandler(header);
}

private float startY;
private float startX;
// 记录viewPager是否拖拽的标记
private boolean mIsHorizontalMove;
// 记录事件是否已被分发
private boolean isDeal;
private int mTouchSlop = 0;

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// 记录手指按下的位置
startY = ev.getY();
startX = ev.getX();
// 初始化标记
mIsHorizontalMove = false;
isDeal = false;
break;
case MotionEvent.ACTION_MOVE:
// 如果已经判断出是否由横向还是纵向处理,则跳出
if (isDeal) {
break;
}
/**拦截禁止交给Ptr的 dispatchTouchEvent处理**/
mIsHorizontalMove = true;
// 获取当前手指位置
float endY = ev.getY();
float endX = ev.getX();
float distanceX = Math.abs(endX - startX);
float distanceY = Math.abs(endY - startY);
if (distanceX != distanceY) {
// 如果X轴位移大于Y轴位移,那么将事件交给viewPager处理
if (distanceX > mTouchSlop && distanceX > distanceY) {
mIsHorizontalMove = true;
isDeal = true;
} else if (distanceY > mTouchSlop) {
mIsHorizontalMove = false;
isDeal = true;
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
//下拉刷新状态时如果滚动了ViewPager 此时mIsHorizontalMove为true 会导致PtrFrameLayout无法恢复原位
//初始化标记
mIsHorizontalMove = false;
isDeal = false;
break;
}
if (mIsHorizontalMove) {
return dispatchTouchEventSupper(ev);
}
return super.dispatchTouchEvent(ev);
}
}

XML布局

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
<me.yezhou.lib.ui_widget.layout.MyPullRefreshFrameLayout
android:id="@+id/layout_pull_refresh"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_order_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<include
android:id="@+id/layout_order_empty"
layout="@layout/layout_order_empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
/>
</LinearLayout>
</me.yezhou.lib.ui_widget.layout.MyPullRefreshFrameLayout>

下拉监听

1
2
3
4
5
6
7
8
9
10
11
12
mLayoutPullRefresh.setPtrHandler(new PtrDefaultHandler() {
@Override
public void onRefreshBegin(PtrFrameLayout frame) {

}

@Override
public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
return super.checkCanDoRefresh(frame, mOrderListRecyclerView == null ? content : mOrderListRecyclerView, header);
//return super.checkCanDoRefresh(frame, 需要刷新的当前View == null ? content : 需要刷新的当前View , header);
}
});

停止刷新

1
mLayoutPullRefresh.refreshComplete();

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :