/
SwipeDragLayout.java
405 lines (357 loc) · 13.2 KB
/
SwipeDragLayout.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package com.ditclear.swipelayout;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
/**
* Created by ditclear on 16/7/12. 可滑动的layout extends FrameLayout
*/
public class SwipeDragLayout extends FrameLayout {
private final String TAG=getClass().getSimpleName();
private static SwipeDragLayout mCacheView;
public final static int DIRECTION_LEFT = 1;
public final static int DIRECTION_RIGHT = 2;
private View contentView;
private View menuView;
private ViewDragHelper mDragHelper;
private Point originPos = new Point();
private boolean isOpen, ios, swipeEnable;
private boolean clickToClose=true;
private int swipeDirection = DIRECTION_LEFT;
private float offsetRatio;
private float needOffset = 0.2f;
private SwipeListener mListener;
//防止多指拖动,当第一个被拖动时,置为true,其它的则不触发拖动
private static boolean isTouching;
public SwipeDragLayout(Context context) {
this(context, null);
}
public SwipeDragLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SwipeDragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SwipeDragLayout);
//最小滑动距离
needOffset = array.getFloat(R.styleable.SwipeDragLayout_need_offset, 0.2f);
//是否有回弹效果
ios = array.getBoolean(R.styleable.SwipeDragLayout_ios, false);
//是否可滑动
swipeEnable = array.getBoolean(R.styleable.SwipeDragLayout_swipe_enable, true);
//滑动方向,默认左滑
swipeDirection = array.getInt(R.styleable.SwipeDragLayout_swipe_direction, DIRECTION_LEFT);
init();
array.recycle();
}
public static SwipeDragLayout getCacheView() {
return mCacheView;
}
//初始化dragHelper,对拖动的view进行操作
private void init() {
mDragHelper = ViewDragHelper.create(this, 1f, new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
//捕获contentView
return child == contentView;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
//滑动距离,如果启动IOS效果,则可滑动4/3倍菜单宽度的距离
if (swipeDirection == DIRECTION_LEFT) {
//左滑
final int leftBound = getPaddingLeft() - (ios ? menuView.getWidth() * 4 / 3
: menuView.getWidth());
final int rightBound = getWidth() - child.getWidth();
return Math.min(Math.max(left, leftBound), rightBound);
} else {
final int leftBound = 0;
final int rightBound = (ios ? menuView.getWidth() * 4 / 3 : menuView.getWidth());
return Math.min(Math.max(left,leftBound),rightBound);
}
}
@Override
public int getViewHorizontalDragRange(View child) {
return contentView == child ? menuView.getWidth() : 0;
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
//fix issue 6 List滚动和Item左右滑动有冲突
// {@link https://github.com/ditclear/SwipeLayout/issues/6}
getParent().requestDisallowInterceptTouchEvent(true);
return super.clampViewPositionVertical(child, top, dy);
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
final int childWidth = menuView.getWidth();
//offsetRatio can callback here
if (swipeDirection == DIRECTION_LEFT) {
offsetRatio = -(float) (left - getPaddingLeft()) / childWidth;
menuView.setTranslationX(Math.max(-menuView.getWidth(), left));
} else {
offsetRatio = ((float) left) / childWidth;
menuView.setTranslationX(Math.min(menuView.getWidth(), left));
}
if (mListener != null) {
mListener.onUpdate(SwipeDragLayout.this, offsetRatio, left);
}
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
//松手时,判断打开还是关闭
if (swipeDirection == DIRECTION_LEFT) {
autoLeft();
} else {
autoRight();
}
}
});
}
private void autoRight() {
if (isOpen) {
if (offsetRatio != 1 && offsetRatio > (1 - needOffset)) {
open();
} else if (offsetRatio == 1) {
if (clickToClose) {
close();
if (mListener != null) {
mListener.onClosed(SwipeDragLayout.this);
}
}
} else {
close();
if (mListener != null) {
mListener.onClosed(SwipeDragLayout.this);
}
}
} else {
if (offsetRatio != 0 && offsetRatio < needOffset) {
close();
} else if (offsetRatio == 0) {
getParent().requestDisallowInterceptTouchEvent(false);
} else {
open();
if (mListener != null) {
mListener.onOpened(SwipeDragLayout.this);
}
}
}
}
public int getSwipeDirection() {
return swipeDirection;
}
public void setSwipeDirection(int swipeDirection) {
if (this.swipeDirection != swipeDirection) {
this.swipeDirection = swipeDirection;
requestLayout();
}
}
public void setSwipeEnable(boolean swipeEnable) {
this.swipeEnable = swipeEnable;
if (!swipeEnable&&mCacheView!=null){
mCacheView.close();
mCacheView = null;
}
}
public void setNeedOffset(float needOffset) {
this.needOffset = needOffset;
}
private void autoLeft() {
if (isOpen()) {
if (offsetRatio != 1 && offsetRatio > (1 - needOffset)) {
open();
} else if (offsetRatio == 1) {
if (clickToClose) {
close();
if (mListener != null) {
mListener.onClosed(SwipeDragLayout.this);
}
}
} else {
close();
if (mListener != null) {
mListener.onClosed(SwipeDragLayout.this);
}
}
} else {
if (offsetRatio != 0 && offsetRatio < needOffset) {
close();
} else if (offsetRatio == 0) {
getParent().requestDisallowInterceptTouchEvent(false);
} else {
open();
if (mListener != null) {
mListener.onOpened(SwipeDragLayout.this);
}
}
}
}
public void setIos(boolean ios) {
this.ios = ios;
}
public boolean isOpen() {
return isOpen;
}
public void open() {
mCacheView = SwipeDragLayout.this;
if (swipeDirection==DIRECTION_LEFT) {
mDragHelper.smoothSlideViewTo(contentView, originPos.x - menuView.getWidth(),
originPos.y);
}else {
mDragHelper.smoothSlideViewTo(contentView, originPos.x + menuView.getWidth(),
originPos.y);
}
isOpen = true;
invalidate();
}
public void close() {
mDragHelper.smoothSlideViewTo(contentView, originPos.x, originPos.y);
isOpen = false;
mCacheView=null;
invalidate();
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
//防止多指拖拽
case MotionEvent.ACTION_DOWN:
if (isTouching){
return false;
}else {
isTouching=true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
if (isTouching){
isTouching=false;
}
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mCacheView != null) {
getParent().requestDisallowInterceptTouchEvent(true);
if (mCacheView != this) {
mCacheView.close();
mCacheView = null;
return true;
} else if (isOpen && mDragHelper.isViewUnder(contentView, (int) ev.getX(),
(int) ev.getY())) {
//open时,不触发contentView的点击事件
getParent().requestDisallowInterceptTouchEvent(true);
return true;
}
}
break;
}
if (swipeEnable) {
return mDragHelper.shouldInterceptTouchEvent(ev);
}else {
return super.onInterceptTouchEvent(ev);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (swipeEnable) {
mDragHelper.processTouchEvent(event);
return true;
} else {
return super.onTouchEvent(event);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
originPos.x = contentView.getLeft();
originPos.y = contentView.getTop();
if (DIRECTION_LEFT == swipeDirection) {
//左滑
menuView.layout(contentView.getWidth(), menuView.getTop(),
contentView.getWidth() + menuView.getWidth(), menuView.getBottom());
} else {
//右滑
menuView.layout(-menuView.getWidth(), menuView.getTop(), originPos.x
, menuView.getBottom());
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount()!=2){
throw new UnsupportedOperationException("子View暂只支持两个");
}
contentView = getChildAt(0);
menuView = getChildAt(1);
if (swipeDirection == DIRECTION_LEFT) {
FrameLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.END;
menuView.setLayoutParams(params);
if (isInEditMode()) {
menuView.layout(contentView.getWidth(), menuView.getTop(),
contentView.getWidth() + menuView.getWidth(), menuView.getBottom());
}
} else if (isInEditMode()) {
menuView.layout(-menuView.getWidth(), menuView.getTop(), contentView.getLeft()
, menuView.getBottom());
}
}
@Override
public void computeScroll() {
if (mDragHelper.continueSettling(true)) {
invalidate();
}
}
@Override
protected void onDetachedFromWindow() {
if (mCacheView == this) {
mCacheView.close();
mCacheView = null;
}
if (menuView!=null&&menuView.getTranslationX()!=0){
menuView.setTranslationX(0f);
}
super.onDetachedFromWindow();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
//recyclerView复用时有点问题,需加以下代码确保layout完全关闭
if (menuView!=null&&menuView.getTranslationX()!=0){
menuView.setTranslationX(0);
}
}
public void addListener(SwipeListener listener) {
mListener = listener;
}
//滑动监听
public interface SwipeListener {
/**
* 拖动中,可根据offset 进行其他动画
* @param layout
* @param offsetRatio 偏移相对于menu宽度的比例
* @param offset 偏移量px
*/
void onUpdate(SwipeDragLayout layout, float offsetRatio, float offset);
/**
* 展开完成
* @param layout
*/
void onOpened(SwipeDragLayout layout);
/**
* 关闭完成
* @param layout
*/
void onClosed(SwipeDragLayout layout);
}
}