-
Notifications
You must be signed in to change notification settings - Fork 5
/
SwipeRefreshLayoutCompatViewPager.java
66 lines (59 loc) · 2.49 KB
/
SwipeRefreshLayoutCompatViewPager.java
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
package com.actor.myandroidframework.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
/**
* Description: <br />
* 下拉刷新, 解决嵌套中的ViewPager横向滑动时, 非常容易把下拉刷新的小球拉下来的问题, 参考: <a href="https://blog.csdn.net/ding19972431/article/details/82114531" target="_blank">这儿</a>
* <br />
* Author : ldf <br />
* Date : 2019/3/8 on 11:22 <br />
* @version 1.0
*/
public class SwipeRefreshLayoutCompatViewPager extends SwipeRefreshLayout {
private float startX;
private float startY;
private boolean mIsVpDragger;// 记录viewPager是否拖拽的标记
private final int mTouchSlop;
public SwipeRefreshLayoutCompatViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
/**
* @see SwipeRefreshLayout#startDragging(float), 当下拉超过一定像素之后, 就开始下拉刷新逻辑
* 默认8像素
*/
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
startY = ev.getY();
startX = ev.getX();
mIsVpDragger = false;
break;
case MotionEvent.ACTION_MOVE:
// 如果viewpager正在拖拽中,那么不拦截它的事件,直接return false;
if (mIsVpDragger) return false;
// 获取当前手指位置
float endY = ev.getY();
float endX = ev.getX();
float distanceX = Math.abs(endX - startX);
float distanceY = Math.abs(endY - startY);
// 如果X轴位移大于Y轴位移,那么将事件交给viewPager处理。
if (distanceX > mTouchSlop && distanceX > distanceY) {
mIsVpDragger = true;
return false;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mIsVpDragger = false;
break;
}
// 如果是Y轴位移大于X轴,事件交给swipeRefreshLayout处理。
return super.onInterceptTouchEvent(ev);
}
}