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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
package guanaj.com.scrollerdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;
/**
* Created by guanaj on 2017/4/18.
*/
public class EasySwipeMeunLayout extends ViewGroup {
private static final String TAG = "EasySwipeMeunLayout";
private int mScaledTouchSlop;
private int mContentWidth;
private float lastx;
private float lasty;
private float firstx;
private float firsty;
private Scroller mScroller;
private int mRightMenuWidths;
public EasySwipeMeunLayout(Context context) {
this(context, null);
}
public EasySwipeMeunLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public EasySwipeMeunLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
//创建辅助类对象
//用户需滑动的最小的距离才被认为是滑动了,单位像素
ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
mScaledTouchSlop = viewConfiguration.getScaledTouchSlop();
mScroller = new Scroller(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int mHeight = 0;
mContentWidth = 0;
//获取child view 数量
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
childView.setClickable(true);
if (childView.getVisibility() != GONE) {
//测量child view
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
mHeight = Math.max(mHeight, childView.getMeasuredHeight());
if (i == 0) {
//第一个为内容view
mContentWidth = childView.getMeasuredWidth();
} else {
//第二个为左滑按钮
mRightMenuWidths = childView.getMeasuredWidth();
}
}
}
setMeasuredDimension(getPaddingLeft() + getPaddingRight() + mContentWidth,
mHeight + getPaddingTop() + getPaddingBottom());
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
if (childView.getVisibility() != GONE) {
if (i == 0) {
childView.layout(getPaddingLeft(), getPaddingTop(), getPaddingLeft() + childView.getMeasuredWidth(), getPaddingTop() + childView.getMeasuredHeight());
} else {
childView.layout(mContentWidth, getPaddingTop(), mContentWidth + childView.getMeasuredWidth(), getPaddingTop() + childView.getMeasuredHeight());
}
}
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
Log.d(TAG, "dispatchTouchEvent() called with: " + "ev = [" + event + "]");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastx = event.getRawX();
lasty = event.getRawY();
firstx = event.getRawX();
firsty = event.getRawY();
break;
}
case MotionEvent.ACTION_MOVE: {
//对左边界进行处理
float distance = lastx - event.getRawX();
if (Math.abs(distance) > mScaledTouchSlop) {
// 当手指拖动值大于mScaledTouchSlop值时,认为应该进行滚动,拦截子控件的事件
return true;
}
break;
}
}
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
float distance = lastx - event.getRawX();
Log.d(TAG, "onTouchEvent() ACTION_MOVE getScrollX:" + getScrollX());
lastx = event.getRawX();
lasty = event.getRawY();
scrollBy((int) distance, 0);
break;
}
case MotionEvent.ACTION_UP:
if (getScrollX() <= 0) {
//对右边界进行处理,不让其滑出
mScroller.startScroll(getScrollX(), 0, -getScrollX(), 0);
} else if (getScrollX() > 0 && getScrollX() >= mRightMenuWidths / 3) {
//删除按钮滑出区域大于1/3,滑出删除按钮
mScroller.startScroll(getScrollX(), 0, mRightMenuWidths-getScrollX(), 0);
} else {
//删除按钮滑出区域小于1/3,滑回原来的位置
mScroller.startScroll(getScrollX(), 0, -getScrollX(), 0);
}
invalidate();
//通知View重绘-invalidate()->onDraw()->computeScroll()
break;
}
return super.onTouchEvent(event);
}
@Override
public void computeScroll() {
//判断Scroller是否执行完毕:
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
//通知View重绘-invalidate()->onDraw()->computeScroll()
invalidate();
}
}
}
|