Skip to content

Commit 8412d50

Browse files
committed
Support clickToClose attr, if set to true, the opened swipeLayout will auto close when click on it's surface view
1 parent 6e909b0 commit 8412d50

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

demo/src/main/res/layout/sample1.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22

3-
<com.daimajia.swipe.SwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
<com.daimajia.swipe.SwipeLayout
4+
xmlns:android="http://schemas.android.com/apk/res/android"
5+
xmlns:app="http://schemas.android.com/apk/res-auto"
46
android:layout_width="match_parent"
5-
android:layout_height="80dp">
7+
android:layout_height="80dp"
8+
app:clickToClose="true">
69

710
<LinearLayout
811
android:tag="Bottom1"

library/src/main/java/com/daimajia/swipe/SwipeLayout.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class SwipeLayout extends FrameLayout {
7070
private boolean mRightSwipeEnabled = true;
7171
private boolean mTopSwipeEnabled = true;
7272
private boolean mBottomSwipeEnabled = true;
73+
private boolean mClickToClose = true;
7374

7475
public static enum DragEdge {
7576
Left,
@@ -104,6 +105,7 @@ public SwipeLayout(Context context, AttributeSet attrs, int defStyle) {
104105
mRightEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_rightEdgeSwipeOffset, 0);
105106
mTopEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_topEdgeSwipeOffset, 0);
106107
mBottomEdgeSwipeOffset = a.getDimension(R.styleable.SwipeLayout_bottomEdgeSwipeOffset, 0);
108+
setClickToClose(a.getBoolean(R.styleable.SwipeLayout_clickToClose, mClickToClose));
107109

108110
mDragEdges = new ArrayList<DragEdge>();
109111
if ((dragEdgeChoices & DRAG_LEFT) == DRAG_LEFT) {
@@ -881,6 +883,13 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
881883
if (!isSwipeEnabled()) {
882884
return false;
883885
}
886+
if(mClickToClose && getOpenStatus() == Status.Open && getSurfaceView()!=null){
887+
Rect rect = new Rect();
888+
getSurfaceView().getHitRect(rect);
889+
if(rect.contains((int)ev.getX(), (int)ev.getY())){
890+
return true;
891+
}
892+
}
884893
for (SwipeDenier denier : mSwipeDeniers) {
885894
if (denier != null && denier.shouldDenySwipe(ev)) {
886895
return false;
@@ -970,6 +979,13 @@ public boolean onTouchEvent(MotionEvent event) {
970979

971980
return super.onTouchEvent(event) || mIsBeingDragged || action == MotionEvent.ACTION_DOWN;
972981
}
982+
public boolean isClickToClose() {
983+
return mClickToClose;
984+
}
985+
986+
public void setClickToClose(boolean mClickToClose) {
987+
this.mClickToClose = mClickToClose;
988+
}
973989

974990
public void setSwipeEnabled(boolean enabled) {
975991
mSwipeEnabled = enabled;
@@ -1102,6 +1118,13 @@ public void setOnLongClickListener(OnLongClickListener l) {
11021118
private GestureDetector gestureDetector = new GestureDetector(getContext(), new SwipeDetector());
11031119

11041120
class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
1121+
@Override
1122+
public boolean onSingleTapUp(MotionEvent e) {
1123+
if(mClickToClose){
1124+
close();
1125+
}
1126+
return super.onSingleTapUp(e);
1127+
}
11051128
@Override
11061129
public boolean onDoubleTap(MotionEvent e) {
11071130
if (mDoubleClickListener != null) {

library/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
<enum name="lay_down" value="0" />
1616
<enum name="pull_out" value="1" />
1717
</attr>
18+
<attr name="clickToClose" format="boolean" />
1819
</declare-styleable>
1920
</resources>

0 commit comments

Comments
 (0)