Skip to content

Commit

Permalink
soarcn#36 swiping down to dismiss could be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
soarcn committed Mar 5, 2015
1 parent 2004d42 commit 7460d4c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
12 changes: 12 additions & 0 deletions library/src/main/java/com/cocosw/bottomsheet/BottomSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class BottomSheet extends Dialog implements DialogInterface {

private int limit = -1;
private boolean cancelOnTouchOutside = true;
private boolean cancelOnSwipeDown = true;

public BottomSheet(Context context) {
super(context,R.style.BottomSheet_Dialog);
Expand Down Expand Up @@ -253,10 +254,21 @@ public void setCanceledOnTouchOutside(boolean cancel) {
cancelOnTouchOutside = cancel;
}

/**
* Sets whether this dialog is canceled when swipe it down
*
* @param cancel
*/
public void setCanceledOnSwipeDown(boolean cancel) {
cancelOnSwipeDown = cancel;
}

private void init(final Context context) {
setCanceledOnTouchOutside(cancelOnTouchOutside);
final ClosableSlidingLayout mDialogView = (ClosableSlidingLayout) View.inflate(context, R.layout.bottom_sheet_dialog, null);
setContentView(mDialogView);
if (!cancelOnSwipeDown)
mDialogView.swipeable = cancelOnSwipeDown;
mDialogView.setSlideListener(new ClosableSlidingLayout.SlideListener() {
@Override
public void onClosed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.FrameLayout;

Expand All @@ -34,6 +33,8 @@ class ClosableSlidingLayout extends FrameLayout {
private boolean collapsible = false;
private float yDiff;

boolean swipeable = true;

public ClosableSlidingLayout(Context context) {
this(context, null);
}
Expand All @@ -46,7 +47,7 @@ public ClosableSlidingLayout(Context context, AttributeSet attrs) {
public ClosableSlidingLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mDragHelper = ViewDragHelper.create(this, 0.8f, new ViewDragCallback());
MINVEL = getResources().getDisplayMetrics().density*400;
MINVEL = getResources().getDisplayMetrics().density * 400;
}

@Override
Expand Down Expand Up @@ -91,14 +92,10 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
return false;
}
yDiff = y - mInitialMotionY;
if (yDiff > mDragHelper.getTouchSlop() && !mIsBeingDragged) {
if (swipeable && yDiff > mDragHelper.getTouchSlop() && !mIsBeingDragged) {
mIsBeingDragged = true;
mDragHelper.captureChildView(getChildAt(0), 0);
}
// else if(yDiff < -mDragHelper.getTouchSlop() && !mIsBeingDragged) {
// if (mListener!=null) mListener.onOpened();
// }

break;
}
mDragHelper.shouldInterceptTouchEvent(event);
Expand All @@ -112,7 +109,7 @@ public void requestDisallowInterceptTouchEvent(boolean b) {

/**
* @return Whether it is possible for the child view of this layout to
* scroll up. Override this if the child view is a custom view.
* scroll up. Override this if the child view is a custom view.
*/
private boolean canChildScrollUp() {
if (android.os.Build.VERSION.SDK_INT < 14) {
Expand Down Expand Up @@ -144,8 +141,10 @@ public boolean onTouchEvent(MotionEvent ev) {
}

try {
mDragHelper.processTouchEvent(ev);
} catch (Exception ignored){}
if (swipeable)
mDragHelper.processTouchEvent(ev);
} catch (Exception ignored) {
}
return true;
}

Expand All @@ -156,7 +155,7 @@ public void computeScroll() {
}
}

public void setSlideListener(SlideListener listener){
public void setSlideListener(SlideListener listener) {
mListener = listener;
}

Expand All @@ -165,9 +164,9 @@ void setCollapsible(boolean collapsible) {
}

/**
*Callback
* Callback
*/
private class ViewDragCallback extends ViewDragHelper.Callback{
private class ViewDragCallback extends ViewDragHelper.Callback {


@Override
Expand All @@ -178,9 +177,9 @@ public boolean tryCaptureView(View child, int pointerId) {
@Override
public void onViewDragStateChanged(int state) {
if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE && dismissed) {
if (mListener!=null) {
mListener.onClosed();
}
if (mListener != null) {
mListener.onClosed();
}
}
}

Expand All @@ -199,10 +198,10 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) {
// }
// } else
if (yvel > MINVEL) {
dismiss(releasedChild,yvel);
dismiss(releasedChild, yvel);
} else {
if (releasedChild.getTop() >= top+height/2) {
dismiss(releasedChild,yvel);
if (releasedChild.getTop() >= top + height / 2) {
dismiss(releasedChild, yvel);
} else {
mDragHelper.smoothSlideViewTo(releasedChild, 0, top);
}
Expand All @@ -212,7 +211,7 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) {

@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
invalidate();
}
}
Expand All @@ -222,12 +221,12 @@ public int clampViewPositionVertical(View child, int top, int dy) {
// if (collapsible) {
// return top;
// } else
return Math.max(top, ClosableSlidingLayout.this.top);
return Math.max(top, ClosableSlidingLayout.this.top);
}
}

private void expand(View releasedChild, float yvel) {
if (mListener!=null) {
if (mListener != null) {
mListener.onOpened();
}
}
Expand All @@ -243,8 +242,9 @@ private void dismiss(View view, float yvel) {
/**
* set listener
*/
interface SlideListener{
interface SlideListener {
void onClosed();

void onOpened();
}

Expand Down

0 comments on commit 7460d4c

Please sign in to comment.