-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerScrollView.java
More file actions
123 lines (106 loc) · 3.44 KB
/
CustomerScrollView.java
File metadata and controls
123 lines (106 loc) · 3.44 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
/**
* TODO: document your custom view class.
*/
public class CustomerScrollView extends ScrollView {
Context mContext;
private View mView;
private float touchY;
private int scrollY = 0;
private boolean handleStop = false;
private int eachStep = 0;
private static final int MAX_SCROLL_HEIGHT = 200;// 最大滑动距离
private static final float SCROLL_RATIO = 0.4f;// 阻尼系数,越小阻力就越大
public CustomerScrollView(Context context) {
super(context);
this.mContext = context;
}
public CustomerScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
public CustomerScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
}
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
this.mView = getChildAt(0);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (arg0.getAction() == MotionEvent.ACTION_DOWN) {
touchY = arg0.getY();
}
return super.onInterceptTouchEvent(arg0);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mView == null) {
return super.onTouchEvent(ev);
} else {
commonOnTouchEvent(ev);
}
return super.onTouchEvent(ev);
}
private void commonOnTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
if (mView.getScrollY() != 0) {
handleStop = true;
animation();
}
break;
case MotionEvent.ACTION_MOVE:
float nowY = ev.getY();
int deltaY = (int) (touchY - nowY);
touchY = nowY;
if (isNeedMove()) {
int offset = mView.getScrollY();
if (offset < MAX_SCROLL_HEIGHT && offset > -MAX_SCROLL_HEIGHT) {
mView.scrollBy(0, (int) (deltaY * SCROLL_RATIO));
handleStop = false;
}
}
break;
default:
break;
}
}
private boolean isNeedMove() {
int viewHight = mView.getMeasuredHeight();
int srollHight = getHeight();
int offset = viewHight - srollHight;
int scrollY = getScrollY();
if (scrollY == 0 || scrollY == offset) {
return true;
}
return false;
}
private void animation() {
scrollY = mView.getScrollY();
eachStep = scrollY / 10;
resetPositionHandler.sendEmptyMessage(0);
}
Handler resetPositionHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (scrollY != 0 && handleStop) {
scrollY -= eachStep;
if ((eachStep < 0 && scrollY > 0) || (eachStep > 0 && scrollY < 0)) {
scrollY = 0;
}
mView.scrollTo(0, scrollY);
this.sendEmptyMessageDelayed(0, 5);
}
}
;
};
}