diff --git a/anylayer/src/main/java/per/goweii/anylayer/BackgroundView.java b/anylayer/src/main/java/per/goweii/anylayer/BackgroundView.java new file mode 100644 index 0000000..ee95554 --- /dev/null +++ b/anylayer/src/main/java/per/goweii/anylayer/BackgroundView.java @@ -0,0 +1,59 @@ +package per.goweii.anylayer; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.widget.ImageView; + +/** + * Create by cuizhen on {2019/12/3} + *

+ * ▄████▄ █ ██ ██▓▒███████▒ ██░ ██ ▓█████ ███▄ █ + * ▒██▀ ▀█ ██ ▓██▒▓██▒▒ ▒ ▒ ▄▀░▓██░ ██▒▓█ ▀ ██ ▀█ █ + * ▒▓█ ▄ ▓██ ▒██░▒██▒░ ▒ ▄▀▒░ ▒██▀▀██░▒███ ▓██ ▀█ ██▒ + * ▒▓▓▄ ▄██▒▓▓█ ░██░░██░ ▄▀▒ ░░▓█ ░██ ▒▓█ ▄▓██▒ ▐▌██▒ + * ▒ ▓███▀ ░▒▒█████▓ ░██░▒███████▒░▓█▒░██▓░▒████▒██░ ▓██░ + * ░ ░▒ ▒ ░░▒▓▒ ▒ ▒ ░▓ ░▒▒ ▓░▒░▒ ▒ ░░▒░▒░░ ▒░ ░ ▒░ ▒ ▒ + * ░ ▒ ░░▒░ ░ ░ ▒ ░░░▒ ▒ ░ ▒ ▒ ░▒░ ░ ░ ░ ░ ░░ ░ ▒░ + * ░ ░░░ ░ ░ ▒ ░░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ + * ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ + * ░ ░ + */ +public class BackgroundView extends ImageView { + + private OnTouchedListener mOnTouchedListener = null; + + public BackgroundView(Context context) { + this(context, null); + } + + public BackgroundView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public BackgroundView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + switch (ev.getAction()) { + case MotionEvent.ACTION_DOWN: + if (mOnTouchedListener != null) { + mOnTouchedListener.onTouched(); + } + break; + default: + break; + } + return false; + } + + public void setOnTouchedListener(OnTouchedListener onTouchedListener) { + this.mOnTouchedListener = onTouchedListener; + } + + public interface OnTouchedListener { + void onTouched(); + } +} diff --git a/anylayer/src/main/java/per/goweii/anylayer/ContainerLayout.java b/anylayer/src/main/java/per/goweii/anylayer/ContainerLayout.java index cf368f3..21561bf 100644 --- a/anylayer/src/main/java/per/goweii/anylayer/ContainerLayout.java +++ b/anylayer/src/main/java/per/goweii/anylayer/ContainerLayout.java @@ -1,5 +1,6 @@ package per.goweii.anylayer; +import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.util.Log; @@ -36,8 +37,9 @@ public ContainerLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } + @SuppressLint("ClickableViewAccessibility") @Override - public boolean dispatchTouchEvent(MotionEvent ev) { + public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: if (mOnTouchedListener != null) { @@ -47,7 +49,7 @@ public boolean dispatchTouchEvent(MotionEvent ev) { default: break; } - return super.dispatchTouchEvent(ev); + return super.onTouchEvent(ev); } public void setOnTouchedListener(OnTouchedListener onTouchedListener) { diff --git a/anylayer/src/main/java/per/goweii/anylayer/DialogLayer.java b/anylayer/src/main/java/per/goweii/anylayer/DialogLayer.java index 78ebddb..f79e843 100644 --- a/anylayer/src/main/java/per/goweii/anylayer/DialogLayer.java +++ b/anylayer/src/main/java/per/goweii/anylayer/DialogLayer.java @@ -177,14 +177,19 @@ public void onClick(View v) { getViewHolder().getChild().setOnClickListener(null); getViewHolder().getChild().setClickable(false); } - getViewHolder().getChild().setOnTouchedListener(new ContainerLayout.OnTouchedListener() { - @Override - public void onTouched() { - if (getConfig().mOutsideTouchedListener != null) { - getConfig().mOutsideTouchedListener.outsideTouched(); + if (getConfig().mOutsideTouchedToDismiss || getConfig().mOutsideTouchedListener != null) { + getViewHolder().getChild().setOnTouchedListener(new ContainerLayout.OnTouchedListener() { + @Override + public void onTouched() { + if (getConfig().mOutsideTouchedToDismiss) { + dismiss(); + } + if (getConfig().mOutsideTouchedListener != null) { + getConfig().mOutsideTouchedListener.outsideTouched(); + } } - } - }); + }); + } fitContainerToActivityContent(); FrameLayout.LayoutParams contentWrapperParams = (FrameLayout.LayoutParams) getViewHolder().getContentWrapper().getLayoutParams(); contentWrapperParams.width = FrameLayout.LayoutParams.MATCH_PARENT; @@ -600,9 +605,14 @@ public DialogLayer outsideTouched(OutsideTouchedListener listener) { return this; } + public DialogLayer outsideTouchedToDismiss(boolean toDismiss) { + getConfig().mOutsideTouchedToDismiss = toDismiss; + return this; + } + public static class ViewHolder extends DecorLayer.ViewHolder { private FrameLayout mActivityContent; - private ImageView mBackground; + private BackgroundView mBackground; private DragLayout mContentWrapper; private View mContent; @@ -645,14 +655,15 @@ public DragLayout getContentWrapper() { return mContentWrapper; } - public ImageView getBackground() { - return mBackground; + public BackgroundView getBackground() { + return (BackgroundView) mBackground; } } protected static class Config extends DecorLayer.Config { protected boolean mOutsideInterceptTouchEvent = true; protected OutsideTouchedListener mOutsideTouchedListener = null; + protected boolean mOutsideTouchedToDismiss = false; protected AnimatorCreator mBackgroundAnimatorCreator = null; protected AnimatorCreator mContentAnimatorCreator = null; diff --git a/anylayer/src/main/java/per/goweii/anylayer/PopupLayer.java b/anylayer/src/main/java/per/goweii/anylayer/PopupLayer.java index a61e9b3..3ca6204 100644 --- a/anylayer/src/main/java/per/goweii/anylayer/PopupLayer.java +++ b/anylayer/src/main/java/per/goweii/anylayer/PopupLayer.java @@ -121,10 +121,8 @@ public void onPreRemove() { @Override public void onDetach() { - if (mOnScrollChangedListener != null) { - getViewHolder().getParent().getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener); - mOnScrollChangedListener = null; - } + getViewHolder().getParent().getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener); + mOnScrollChangedListener = null; super.onDetach(); } @@ -163,15 +161,19 @@ public void run() { updateLocation(); } }); - if (!getConfig().mOutsideInterceptTouchEvent) { - mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() { - @Override - public void onScrollChanged() { - updateLocation(); + mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() { + @Override + public void onScrollChanged() { + if (getConfig().mViewTreeScrollChangedToDismiss) { + dismiss(); } - }; - getViewHolder().getParent().getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener); - } + if (getConfig().mOnViewTreeScrollChangedListener != null) { + getConfig().mOnViewTreeScrollChangedListener.onScrollChanged(); + } + updateLocation(); + } + }; + getViewHolder().getParent().getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener); } private void initContentWrapperLocation(int targetX, int targetY, int targetWidth, int targetHeight) { @@ -483,7 +485,7 @@ public void updateLocation() { int targetHeight = 0; if (target != null) { targetWidth = target.getWidth(); - targetHeight = target.getWidth(); + targetHeight = target.getHeight(); } initContentWrapperLocation(targetX, targetY, targetWidth, targetHeight); initBackgroundLocation(); @@ -494,6 +496,16 @@ public PopupLayer updateLocationInterceptor(UpdateLocationInterceptor intercepto return this; } + public PopupLayer onViewTreeScrollChangedListener(OnViewTreeScrollChangedListener listener) { + getConfig().mOnViewTreeScrollChangedListener = listener; + return this; + } + + public PopupLayer scrollChangedToDismiss(boolean toDismiss) { + getConfig().mViewTreeScrollChangedToDismiss = toDismiss; + return this; + } + public PopupLayer targetView(View targetView) { getViewHolder().setTarget(targetView); updateLocation(); @@ -674,6 +686,8 @@ public View getTarget() { } protected static class Config extends DialogLayer.Config { + protected OnViewTreeScrollChangedListener mOnViewTreeScrollChangedListener = null; + protected boolean mViewTreeScrollChangedToDismiss = false; protected UpdateLocationInterceptor mUpdateLocationInterceptor = null; protected boolean mContentClip = true; protected boolean mBackgroundAlign = true; @@ -695,4 +709,8 @@ void interceptor(float[] popupXY, int popupWidth, int popupHeight, int targetX, int targetY, int targetWidth, int targetHeight, int parentX, int parentY, int parentWidth, int parentHeight); } + + public interface OnViewTreeScrollChangedListener { + void onScrollChanged(); + } } diff --git a/anylayer/src/main/res/layout/anylayer_dialog_layer.xml b/anylayer/src/main/res/layout/anylayer_dialog_layer.xml index 4f7af39..aeb9e9f 100644 --- a/anylayer/src/main/res/layout/anylayer_dialog_layer.xml +++ b/anylayer/src/main/res/layout/anylayer_dialog_layer.xml @@ -4,7 +4,7 @@ android:layout_width="match_parent" android:layout_height="match_parent"> -