-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathSwipeLayout.java
1660 lines (1459 loc) · 58.8 KB
/
SwipeLayout.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.daimajia.swipe;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class SwipeLayout extends FrameLayout {
@Deprecated
public static final int EMPTY_LAYOUT = -1;
private static final int DRAG_LEFT = 1;
private static final int DRAG_RIGHT = 2;
private static final int DRAG_TOP = 4;
private static final int DRAG_BOTTOM = 8;
private static final DragEdge DefaultDragEdge = DragEdge.Right;
private int mTouchSlop;
private DragEdge mCurrentDragEdge = DefaultDragEdge;
private ViewDragHelper mDragHelper;
private int mDragDistance = 0;
private LinkedHashMap<DragEdge, View> mDragEdges = new LinkedHashMap<>();
private ShowMode mShowMode;
private float[] mEdgeSwipesOffset = new float[4];
private List<SwipeListener> mSwipeListeners = new ArrayList<>();
private List<SwipeDenier> mSwipeDeniers = new ArrayList<>();
private Map<View, ArrayList<OnRevealListener>> mRevealListeners = new HashMap<>();
private Map<View, Boolean> mShowEntirely = new HashMap<>();
private Map<View, Rect> mViewBoundCache = new HashMap<>();//save all children's bound, restore in onLayout
private DoubleClickListener mDoubleClickListener;
private boolean mSwipeEnabled = true;
private boolean[] mSwipesEnabled = new boolean[]{true, true, true, true};
private boolean mClickToClose = false;
private float mWillOpenPercentAfterOpen = 0.75f;
private float mWillOpenPercentAfterClose = 0.25f;
public enum DragEdge {
Left,
Top,
Right,
Bottom
}
public enum ShowMode {
LayDown,
PullOut
}
public SwipeLayout(Context context) {
this(context, null);
}
public SwipeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SwipeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mDragHelper = ViewDragHelper.create(this, mDragHelperCallback);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeLayout);
int dragEdgeChoices = a.getInt(R.styleable.SwipeLayout_drag_edge, DRAG_RIGHT);
mEdgeSwipesOffset[DragEdge.Left.ordinal()] = a.getDimension(R.styleable.SwipeLayout_leftEdgeSwipeOffset, 0);
mEdgeSwipesOffset[DragEdge.Right.ordinal()] = a.getDimension(R.styleable.SwipeLayout_rightEdgeSwipeOffset, 0);
mEdgeSwipesOffset[DragEdge.Top.ordinal()] = a.getDimension(R.styleable.SwipeLayout_topEdgeSwipeOffset, 0);
mEdgeSwipesOffset[DragEdge.Bottom.ordinal()] = a.getDimension(R.styleable.SwipeLayout_bottomEdgeSwipeOffset, 0);
setClickToClose(a.getBoolean(R.styleable.SwipeLayout_clickToClose, mClickToClose));
if ((dragEdgeChoices & DRAG_LEFT) == DRAG_LEFT) {
mDragEdges.put(DragEdge.Left, null);
}
if ((dragEdgeChoices & DRAG_TOP) == DRAG_TOP) {
mDragEdges.put(DragEdge.Top, null);
}
if ((dragEdgeChoices & DRAG_RIGHT) == DRAG_RIGHT) {
mDragEdges.put(DragEdge.Right, null);
}
if ((dragEdgeChoices & DRAG_BOTTOM) == DRAG_BOTTOM) {
mDragEdges.put(DragEdge.Bottom, null);
}
int ordinal = a.getInt(R.styleable.SwipeLayout_show_mode, ShowMode.PullOut.ordinal());
mShowMode = ShowMode.values()[ordinal];
a.recycle();
}
public interface SwipeListener {
void onStartOpen(SwipeLayout layout);
void onOpen(SwipeLayout layout);
void onStartClose(SwipeLayout layout);
void onClose(SwipeLayout layout);
void onUpdate(SwipeLayout layout, int leftOffset, int topOffset);
void onHandRelease(SwipeLayout layout, float xvel, float yvel);
}
public void addSwipeListener(SwipeListener l) {
mSwipeListeners.add(l);
}
public void removeSwipeListener(SwipeListener l) {
mSwipeListeners.remove(l);
}
public void removeAllSwipeListener() {
mSwipeListeners.clear();
}
public interface SwipeDenier {
/*
* Called in onInterceptTouchEvent Determines if this swipe event should
* be denied Implement this interface if you are using views with swipe
* gestures As a child of SwipeLayout
*
* @return true deny false allow
*/
boolean shouldDenySwipe(MotionEvent ev);
}
public void addSwipeDenier(SwipeDenier denier) {
mSwipeDeniers.add(denier);
}
public void removeSwipeDenier(SwipeDenier denier) {
mSwipeDeniers.remove(denier);
}
public void removeAllSwipeDeniers() {
mSwipeDeniers.clear();
}
public interface OnRevealListener {
void onReveal(View child, DragEdge edge, float fraction, int distance);
}
/**
* bind a view with a specific
* {@link com.daimajia.swipe.SwipeLayout.OnRevealListener}
*
* @param childId the view id.
* @param l the target
* {@link com.daimajia.swipe.SwipeLayout.OnRevealListener}
*/
public void addRevealListener(int childId, OnRevealListener l) {
View child = findViewById(childId);
if (child == null) {
throw new IllegalArgumentException("Child does not belong to SwipeListener.");
}
if (!mShowEntirely.containsKey(child)) {
mShowEntirely.put(child, false);
}
if (mRevealListeners.get(child) == null)
mRevealListeners.put(child, new ArrayList<OnRevealListener>());
mRevealListeners.get(child).add(l);
}
/**
* bind multiple views with an
* {@link com.daimajia.swipe.SwipeLayout.OnRevealListener}.
*
* @param childIds the view id.
* @param l the {@link com.daimajia.swipe.SwipeLayout.OnRevealListener}
*/
public void addRevealListener(int[] childIds, OnRevealListener l) {
for (int i : childIds)
addRevealListener(i, l);
}
public void removeRevealListener(int childId, OnRevealListener l) {
View child = findViewById(childId);
if (child == null) return;
mShowEntirely.remove(child);
if (mRevealListeners.containsKey(child)) mRevealListeners.get(child).remove(l);
}
public void removeAllRevealListeners(int childId) {
View child = findViewById(childId);
if (child != null) {
mRevealListeners.remove(child);
mShowEntirely.remove(child);
}
}
private ViewDragHelper.Callback mDragHelperCallback = new ViewDragHelper.Callback() {
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child == getSurfaceView()) {
switch (mCurrentDragEdge) {
case Top:
case Bottom:
return getPaddingLeft();
case Left:
if (left < getPaddingLeft()) return getPaddingLeft();
if (left > getPaddingLeft() + mDragDistance)
return getPaddingLeft() + mDragDistance;
break;
case Right:
if (left > getPaddingLeft()) return getPaddingLeft();
if (left < getPaddingLeft() - mDragDistance)
return getPaddingLeft() - mDragDistance;
break;
}
} else if (getCurrentBottomView() == child) {
switch (mCurrentDragEdge) {
case Top:
case Bottom:
return getPaddingLeft();
case Left:
if (mShowMode == ShowMode.PullOut) {
if (left > getPaddingLeft()) return getPaddingLeft();
}
break;
case Right:
if (mShowMode == ShowMode.PullOut) {
if (left < getMeasuredWidth() - mDragDistance) {
return getMeasuredWidth() - mDragDistance;
}
}
break;
}
}
return left;
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
if (child == getSurfaceView()) {
switch (mCurrentDragEdge) {
case Left:
case Right:
return getPaddingTop();
case Top:
if (top < getPaddingTop()) return getPaddingTop();
if (top > getPaddingTop() + mDragDistance)
return getPaddingTop() + mDragDistance;
break;
case Bottom:
if (top < getPaddingTop() - mDragDistance) {
return getPaddingTop() - mDragDistance;
}
if (top > getPaddingTop()) {
return getPaddingTop();
}
}
} else {
View surfaceView = getSurfaceView();
int surfaceViewTop = surfaceView == null ? 0 : surfaceView.getTop();
switch (mCurrentDragEdge) {
case Left:
case Right:
return getPaddingTop();
case Top:
if (mShowMode == ShowMode.PullOut) {
if (top > getPaddingTop()) return getPaddingTop();
} else {
if (surfaceViewTop + dy < getPaddingTop())
return getPaddingTop();
if (surfaceViewTop + dy > getPaddingTop() + mDragDistance)
return getPaddingTop() + mDragDistance;
}
break;
case Bottom:
if (mShowMode == ShowMode.PullOut) {
if (top < getMeasuredHeight() - mDragDistance)
return getMeasuredHeight() - mDragDistance;
} else {
if (surfaceViewTop + dy >= getPaddingTop())
return getPaddingTop();
if (surfaceViewTop + dy <= getPaddingTop() - mDragDistance)
return getPaddingTop() - mDragDistance;
}
}
}
return top;
}
@Override
public boolean tryCaptureView(View child, int pointerId) {
boolean result = child == getSurfaceView() || getBottomViews().contains(child);
if (result) {
isCloseBeforeDrag = getOpenStatus() == Status.Close;
}
return result;
}
@Override
public int getViewHorizontalDragRange(View child) {
return mDragDistance;
}
@Override
public int getViewVerticalDragRange(View child) {
return mDragDistance;
}
boolean isCloseBeforeDrag = true;
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
processHandRelease(xvel, yvel, isCloseBeforeDrag);
for (SwipeListener l : mSwipeListeners) {
l.onHandRelease(SwipeLayout.this, xvel, yvel);
}
invalidate();
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
View surfaceView = getSurfaceView();
if (surfaceView == null) return;
View currentBottomView = getCurrentBottomView();
int evLeft = surfaceView.getLeft(),
evRight = surfaceView.getRight(),
evTop = surfaceView.getTop(),
evBottom = surfaceView.getBottom();
if (changedView == surfaceView) {
if (mShowMode == ShowMode.PullOut && currentBottomView != null) {
if (mCurrentDragEdge == DragEdge.Left || mCurrentDragEdge == DragEdge.Right) {
currentBottomView.offsetLeftAndRight(dx);
} else {
currentBottomView.offsetTopAndBottom(dy);
}
}
} else if (getBottomViews().contains(changedView)) {
if (mShowMode == ShowMode.PullOut) {
surfaceView.offsetLeftAndRight(dx);
surfaceView.offsetTopAndBottom(dy);
} else {
Rect rect = computeBottomLayDown(mCurrentDragEdge);
if (currentBottomView != null) {
currentBottomView.layout(rect.left, rect.top, rect.right, rect.bottom);
}
int newLeft = surfaceView.getLeft() + dx, newTop = surfaceView.getTop() + dy;
if (mCurrentDragEdge == DragEdge.Left && newLeft < getPaddingLeft())
newLeft = getPaddingLeft();
else if (mCurrentDragEdge == DragEdge.Right && newLeft > getPaddingLeft())
newLeft = getPaddingLeft();
else if (mCurrentDragEdge == DragEdge.Top && newTop < getPaddingTop())
newTop = getPaddingTop();
else if (mCurrentDragEdge == DragEdge.Bottom && newTop > getPaddingTop())
newTop = getPaddingTop();
surfaceView.layout(newLeft, newTop, newLeft + getMeasuredWidth(), newTop + getMeasuredHeight());
}
}
dispatchRevealEvent(evLeft, evTop, evRight, evBottom);
dispatchSwipeEvent(evLeft, evTop, dx, dy);
invalidate();
captureChildrenBound();
}
};
/**
* save children's bounds, so they can restore the bound in {@link #onLayout(boolean, int, int, int, int)}
*/
private void captureChildrenBound() {
View currentBottomView = getCurrentBottomView();
if (getOpenStatus() == Status.Close) {
mViewBoundCache.remove(currentBottomView);
return;
}
View[] views = new View[]{getSurfaceView(), currentBottomView};
for (View child : views) {
Rect rect = mViewBoundCache.get(child);
if (rect == null) {
rect = new Rect();
mViewBoundCache.put(child, rect);
}
rect.left = child.getLeft();
rect.top = child.getTop();
rect.right = child.getRight();
rect.bottom = child.getBottom();
}
}
/**
* the dispatchRevealEvent method may not always get accurate position, it
* makes the view may not always get the event when the view is totally
* show( fraction = 1), so , we need to calculate every time.
*/
protected boolean isViewTotallyFirstShowed(View child, Rect relativePosition, DragEdge edge, int surfaceLeft,
int surfaceTop, int surfaceRight, int surfaceBottom) {
if (mShowEntirely.get(child)) return false;
int childLeft = relativePosition.left;
int childRight = relativePosition.right;
int childTop = relativePosition.top;
int childBottom = relativePosition.bottom;
boolean r = false;
if (getShowMode() == ShowMode.LayDown) {
if ((edge == DragEdge.Right && surfaceRight <= childLeft)
|| (edge == DragEdge.Left && surfaceLeft >= childRight)
|| (edge == DragEdge.Top && surfaceTop >= childBottom)
|| (edge == DragEdge.Bottom && surfaceBottom <= childTop)) r = true;
} else if (getShowMode() == ShowMode.PullOut) {
if ((edge == DragEdge.Right && childRight <= getWidth())
|| (edge == DragEdge.Left && childLeft >= getPaddingLeft())
|| (edge == DragEdge.Top && childTop >= getPaddingTop())
|| (edge == DragEdge.Bottom && childBottom <= getHeight())) r = true;
}
return r;
}
protected boolean isViewShowing(View child, Rect relativePosition, DragEdge availableEdge, int surfaceLeft,
int surfaceTop, int surfaceRight, int surfaceBottom) {
int childLeft = relativePosition.left;
int childRight = relativePosition.right;
int childTop = relativePosition.top;
int childBottom = relativePosition.bottom;
if (getShowMode() == ShowMode.LayDown) {
switch (availableEdge) {
case Right:
if (surfaceRight > childLeft && surfaceRight <= childRight) {
return true;
}
break;
case Left:
if (surfaceLeft < childRight && surfaceLeft >= childLeft) {
return true;
}
break;
case Top:
if (surfaceTop >= childTop && surfaceTop < childBottom) {
return true;
}
break;
case Bottom:
if (surfaceBottom > childTop && surfaceBottom <= childBottom) {
return true;
}
break;
}
} else if (getShowMode() == ShowMode.PullOut) {
switch (availableEdge) {
case Right:
if (childLeft <= getWidth() && childRight > getWidth()) return true;
break;
case Left:
if (childRight >= getPaddingLeft() && childLeft < getPaddingLeft()) return true;
break;
case Top:
if (childTop < getPaddingTop() && childBottom >= getPaddingTop()) return true;
break;
case Bottom:
if (childTop < getHeight() && childTop >= getPaddingTop()) return true;
break;
}
}
return false;
}
protected Rect getRelativePosition(View child) {
View t = child;
Rect r = new Rect(t.getLeft(), t.getTop(), 0, 0);
while (t.getParent() != null && t != getRootView()) {
t = (View) t.getParent();
if (t == this) break;
r.left += t.getLeft();
r.top += t.getTop();
}
r.right = r.left + child.getMeasuredWidth();
r.bottom = r.top + child.getMeasuredHeight();
return r;
}
private int mEventCounter = 0;
protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, int dx, int dy) {
DragEdge edge = getDragEdge();
boolean open = true;
if (edge == DragEdge.Left) {
if (dx < 0) open = false;
} else if (edge == DragEdge.Right) {
if (dx > 0) open = false;
} else if (edge == DragEdge.Top) {
if (dy < 0) open = false;
} else if (edge == DragEdge.Bottom) {
if (dy > 0) open = false;
}
dispatchSwipeEvent(surfaceLeft, surfaceTop, open);
}
protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) {
safeBottomView();
Status status = getOpenStatus();
if (!mSwipeListeners.isEmpty()) {
mEventCounter++;
for (SwipeListener l : mSwipeListeners) {
if (mEventCounter == 1) {
if (open) {
l.onStartOpen(this);
} else {
l.onStartClose(this);
}
}
l.onUpdate(SwipeLayout.this, surfaceLeft - getPaddingLeft(), surfaceTop - getPaddingTop());
}
if (status == Status.Close) {
for (SwipeListener l : mSwipeListeners) {
l.onClose(SwipeLayout.this);
}
mEventCounter = 0;
}
if (status == Status.Open) {
View currentBottomView = getCurrentBottomView();
if (currentBottomView != null) {
currentBottomView.setEnabled(true);
}
for (SwipeListener l : mSwipeListeners) {
l.onOpen(SwipeLayout.this);
}
mEventCounter = 0;
}
}
}
/**
* prevent bottom view get any touch event. Especially in LayDown mode.
*/
private void safeBottomView() {
Status status = getOpenStatus();
List<View> bottoms = getBottomViews();
if (status == Status.Close) {
for (View bottom : bottoms) {
if (bottom != null && bottom.getVisibility() != INVISIBLE) {
bottom.setVisibility(INVISIBLE);
}
}
} else {
View currentBottomView = getCurrentBottomView();
if (currentBottomView != null && currentBottomView.getVisibility() != VISIBLE) {
currentBottomView.setVisibility(VISIBLE);
}
}
}
protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, final int surfaceRight,
final int surfaceBottom) {
if (mRevealListeners.isEmpty()) return;
for (Map.Entry<View, ArrayList<OnRevealListener>> entry : mRevealListeners.entrySet()) {
View child = entry.getKey();
Rect rect = getRelativePosition(child);
if (isViewShowing(child, rect, mCurrentDragEdge, surfaceLeft, surfaceTop,
surfaceRight, surfaceBottom)) {
mShowEntirely.put(child, false);
int distance = 0;
float fraction = 0f;
if (getShowMode() == ShowMode.LayDown) {
switch (mCurrentDragEdge) {
case Left:
distance = rect.left - surfaceLeft;
fraction = distance / (float) child.getWidth();
break;
case Right:
distance = rect.right - surfaceRight;
fraction = distance / (float) child.getWidth();
break;
case Top:
distance = rect.top - surfaceTop;
fraction = distance / (float) child.getHeight();
break;
case Bottom:
distance = rect.bottom - surfaceBottom;
fraction = distance / (float) child.getHeight();
break;
}
} else if (getShowMode() == ShowMode.PullOut) {
switch (mCurrentDragEdge) {
case Left:
distance = rect.right - getPaddingLeft();
fraction = distance / (float) child.getWidth();
break;
case Right:
distance = rect.left - getWidth();
fraction = distance / (float) child.getWidth();
break;
case Top:
distance = rect.bottom - getPaddingTop();
fraction = distance / (float) child.getHeight();
break;
case Bottom:
distance = rect.top - getHeight();
fraction = distance / (float) child.getHeight();
break;
}
}
for (OnRevealListener l : entry.getValue()) {
l.onReveal(child, mCurrentDragEdge, Math.abs(fraction), distance);
if (Math.abs(fraction) == 1) {
mShowEntirely.put(child, true);
}
}
}
if (isViewTotallyFirstShowed(child, rect, mCurrentDragEdge, surfaceLeft, surfaceTop,
surfaceRight, surfaceBottom)) {
mShowEntirely.put(child, true);
for (OnRevealListener l : entry.getValue()) {
if (mCurrentDragEdge == DragEdge.Left
|| mCurrentDragEdge == DragEdge.Right)
l.onReveal(child, mCurrentDragEdge, 1, child.getWidth());
else
l.onReveal(child, mCurrentDragEdge, 1, child.getHeight());
}
}
}
}
@Override
public void computeScroll() {
super.computeScroll();
if (mDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
/**
* {@link android.view.View.OnLayoutChangeListener} added in API 11. I need
* to support it from API 8.
*/
public interface OnLayout {
void onLayout(SwipeLayout v);
}
private List<OnLayout> mOnLayoutListeners;
public void addOnLayoutListener(OnLayout l) {
if (mOnLayoutListeners == null) mOnLayoutListeners = new ArrayList<OnLayout>();
mOnLayoutListeners.add(l);
}
public void removeOnLayoutListener(OnLayout l) {
if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l);
}
public void clearDragEdge() {
mDragEdges.clear();
}
public void setDrag(DragEdge dragEdge, int childId) {
clearDragEdge();
addDrag(dragEdge, childId);
}
public void setDrag(DragEdge dragEdge, View child) {
clearDragEdge();
addDrag(dragEdge, child);
}
public void addDrag(DragEdge dragEdge, int childId) {
addDrag(dragEdge, findViewById(childId), null);
}
public void addDrag(DragEdge dragEdge, View child) {
addDrag(dragEdge, child, null);
}
public void addDrag(DragEdge dragEdge, View child, ViewGroup.LayoutParams params) {
if (child == null) return;
if (params == null) {
params = generateDefaultLayoutParams();
}
if (!checkLayoutParams(params)) {
params = generateLayoutParams(params);
}
int gravity = -1;
switch (dragEdge) {
case Left:
gravity = Gravity.LEFT;
break;
case Right:
gravity = Gravity.RIGHT;
break;
case Top:
gravity = Gravity.TOP;
break;
case Bottom:
gravity = Gravity.BOTTOM;
break;
}
if (params instanceof FrameLayout.LayoutParams) {
((LayoutParams) params).gravity = gravity;
}
addView(child, 0, params);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child == null) return;
int gravity = Gravity.NO_GRAVITY;
try {
gravity = (Integer) params.getClass().getField("gravity").get(params);
} catch (Exception e) {
e.printStackTrace();
}
if (gravity > 0) {
gravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));
if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
mDragEdges.put(DragEdge.Left, child);
}
if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
mDragEdges.put(DragEdge.Right, child);
}
if ((gravity & Gravity.TOP) == Gravity.TOP) {
mDragEdges.put(DragEdge.Top, child);
}
if ((gravity & Gravity.BOTTOM) == Gravity.BOTTOM) {
mDragEdges.put(DragEdge.Bottom, child);
}
} else {
for (Map.Entry<DragEdge, View> entry : mDragEdges.entrySet()) {
if (entry.getValue() == null) {
//means used the drag_edge attr, the no gravity child should be use set
mDragEdges.put(entry.getKey(), child);
break;
}
}
}
if (child.getParent() == this) {
return;
}
super.addView(child, index, params);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
updateBottomViews();
if (mOnLayoutListeners != null) for (int i = 0; i < mOnLayoutListeners.size(); i++) {
mOnLayoutListeners.get(i).onLayout(this);
}
}
void layoutPullOut() {
View surfaceView = getSurfaceView();
Rect surfaceRect = mViewBoundCache.get(surfaceView);
if (surfaceRect == null) surfaceRect = computeSurfaceLayoutArea(false);
if (surfaceView != null) {
surfaceView.layout(surfaceRect.left, surfaceRect.top, surfaceRect.right, surfaceRect.bottom);
bringChildToFront(surfaceView);
}
View currentBottomView = getCurrentBottomView();
Rect bottomViewRect = mViewBoundCache.get(currentBottomView);
if (bottomViewRect == null)
bottomViewRect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, surfaceRect);
if (currentBottomView != null) {
currentBottomView.layout(bottomViewRect.left, bottomViewRect.top, bottomViewRect.right, bottomViewRect.bottom);
}
}
void layoutLayDown() {
View surfaceView = getSurfaceView();
Rect surfaceRect = mViewBoundCache.get(surfaceView);
if (surfaceRect == null) surfaceRect = computeSurfaceLayoutArea(false);
if (surfaceView != null) {
surfaceView.layout(surfaceRect.left, surfaceRect.top, surfaceRect.right, surfaceRect.bottom);
bringChildToFront(surfaceView);
}
View currentBottomView = getCurrentBottomView();
Rect bottomViewRect = mViewBoundCache.get(currentBottomView);
if (bottomViewRect == null)
bottomViewRect = computeBottomLayoutAreaViaSurface(ShowMode.LayDown, surfaceRect);
if (currentBottomView != null) {
currentBottomView.layout(bottomViewRect.left, bottomViewRect.top, bottomViewRect.right, bottomViewRect.bottom);
}
}
private boolean mIsBeingDragged;
private void checkCanDrag(MotionEvent ev) {
if (mIsBeingDragged) return;
if (getOpenStatus() == Status.Middle) {
mIsBeingDragged = true;
return;
}
Status status = getOpenStatus();
float distanceX = ev.getRawX() - sX;
float distanceY = ev.getRawY() - sY;
float angle = Math.abs(distanceY / distanceX);
angle = (float) Math.toDegrees(Math.atan(angle));
if (getOpenStatus() == Status.Close) {
DragEdge dragEdge;
if (angle < 45) {
if (distanceX > 0 && isLeftSwipeEnabled()) {
dragEdge = DragEdge.Left;
} else if (distanceX < 0 && isRightSwipeEnabled()) {
dragEdge = DragEdge.Right;
} else return;
} else {
if (distanceY > 0 && isTopSwipeEnabled()) {
dragEdge = DragEdge.Top;
} else if (distanceY < 0 && isBottomSwipeEnabled()) {
dragEdge = DragEdge.Bottom;
} else return;
}
setCurrentDragEdge(dragEdge);
}
boolean doNothing = false;
if (mCurrentDragEdge == DragEdge.Right) {
boolean suitable = (status == Status.Open && distanceX > mTouchSlop)
|| (status == Status.Close && distanceX < -mTouchSlop);
suitable = suitable || (status == Status.Middle);
if (angle > 30 || !suitable) {
doNothing = true;
}
}
if (mCurrentDragEdge == DragEdge.Left) {
boolean suitable = (status == Status.Open && distanceX < -mTouchSlop)
|| (status == Status.Close && distanceX > mTouchSlop);
suitable = suitable || status == Status.Middle;
if (angle > 30 || !suitable) {
doNothing = true;
}
}
if (mCurrentDragEdge == DragEdge.Top) {
boolean suitable = (status == Status.Open && distanceY < -mTouchSlop)
|| (status == Status.Close && distanceY > mTouchSlop);
suitable = suitable || status == Status.Middle;
if (angle < 60 || !suitable) {
doNothing = true;
}
}
if (mCurrentDragEdge == DragEdge.Bottom) {
boolean suitable = (status == Status.Open && distanceY > mTouchSlop)
|| (status == Status.Close && distanceY < -mTouchSlop);
suitable = suitable || status == Status.Middle;
if (angle < 60 || !suitable) {
doNothing = true;
}
}
mIsBeingDragged = !doNothing;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!isSwipeEnabled()) {
return false;
}
if (mClickToClose && getOpenStatus() == Status.Open && isTouchOnSurface(ev)) {
return true;
}
for (SwipeDenier denier : mSwipeDeniers) {
if (denier != null && denier.shouldDenySwipe(ev)) {
return false;
}
}
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mDragHelper.processTouchEvent(ev);
mIsBeingDragged = false;
sX = ev.getRawX();
sY = ev.getRawY();
//if the swipe is in middle state(scrolling), should intercept the touch
if (getOpenStatus() == Status.Middle) {
mIsBeingDragged = true;
}
break;
case MotionEvent.ACTION_MOVE:
boolean beforeCheck = mIsBeingDragged;
checkCanDrag(ev);
if (mIsBeingDragged) {
ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
if (!beforeCheck && mIsBeingDragged) {
//let children has one chance to catch the touch, and request the swipe not intercept
//useful when swipeLayout wrap a swipeLayout or other gestural layout
return false;
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mIsBeingDragged = false;
mDragHelper.processTouchEvent(ev);
break;
default://handle other action, such as ACTION_POINTER_DOWN/UP
mDragHelper.processTouchEvent(ev);
}
return mIsBeingDragged;
}
private float sX = -1, sY = -1;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isSwipeEnabled()) return super.onTouchEvent(event);
int action = event.getActionMasked();
gestureDetector.onTouchEvent(event);
switch (action) {
case MotionEvent.ACTION_DOWN:
mDragHelper.processTouchEvent(event);
sX = event.getRawX();
sY = event.getRawY();
case MotionEvent.ACTION_MOVE: {
//the drag state and the direction are already judged at onInterceptTouchEvent
checkCanDrag(event);
if (mIsBeingDragged) {
getParent().requestDisallowInterceptTouchEvent(true);
mDragHelper.processTouchEvent(event);
}
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mIsBeingDragged = false;
mDragHelper.processTouchEvent(event);
break;
default://handle other action, such as ACTION_POINTER_DOWN/UP
mDragHelper.processTouchEvent(event);
}
return super.onTouchEvent(event) || mIsBeingDragged || action == MotionEvent.ACTION_DOWN;
}
public boolean isClickToClose() {
return mClickToClose;
}
public void setClickToClose(boolean mClickToClose) {
this.mClickToClose = mClickToClose;
}
public void setSwipeEnabled(boolean enabled) {