最近在项目中发现一个现象,自定义的DialogFragment 出现后,你去点区域外的边缘,诶,怎么DialogFragment不会消失?非得点一定距离外的位置才会隐藏。这引起了我的好奇心。
一开始的猜测是:难道DialogFragment实际区域比可见区域大?然后我打开了debug.layout的开关。发现实际区域和可见区域是一致的,不存在padding。
在网上翻了一些帖子后,发现原来是它在搞鬼,如下Dialog源码中的onTouchEvent方法:
/**
* Called when a touch screen event was not handled by any of the views
* under it. This is most useful to process touch events that happen outside
* of your window bounds, where there is no view to receive it.
*
* @param event The touch screen event being processed.
* @return Return true if you have consumed the event, false if you haven't.
* The default implementation will cancel the dialog when a touch
* happens outside of the window bounds.
*/
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) {
cancel();
return true;
}
return false;
}
这个方法中会判断Window#showCloseOnTouch(Context context, MotionEvent event)。
我们继续往下跟:
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
public boolean shouldCloseOnTouch(Context context, MotionEvent event) {
final boolean isOutside =
event.getAction() == MotionEvent.ACTION_UP && isOutOfBounds(context, event)
|| event.getAction() == MotionEvent.ACTION_OUTSIDE;
if (mCloseOnTouchOutside && peekDecorView() != null && isOutside) {
return true;
}
return false;
}
一个值得怀疑的方法:isOutOfBounds 映入眼帘,继续往下跟:
private boolean isOutOfBounds(Context context, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
final View decorView = getDecorView();
return (x < -slop) || (y < -slop)
|| (x > (decorView.getWidth()+slop))
|| (y > (decorView.getHeight()+slop));
}
这里有个slop,呐,是它,没跑了。
我们看到,通过
left:-slop;
top:-slop;
right:decorView.getWidth()+slop;
bottom:decorView.getHeight()+slop;
构建了一个比Dialog视图更大的一个区域,只有点到这个区域外,Dialog才会dismiss。
既然原因找到了,那解决思路也有了,去掉这个slop的影响就行了。下面贴出解决方案:
1、自定义一个Dialog,重写onTouchEvent方法;
public class WithoutSlopDialog extends Dialog {
private boolean mCancelable = true;//默认值是true,因为Dialog 默认是可以取消的
public WithoutSlopDialog(@NonNull Context context) {
super(context);
}
public WithoutSlopDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
public WithoutSlopDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
//重写此方法
@Override
public void setCanceledOnTouchOutside(boolean cancel) {
super.setCanceledOnTouchOutside(cancel);
mCancelable = cancel;
}
//重写onTouchEvent方法,用isOutOfBounds取代原本的Window#shouldCloseOnTouch(Context
//context, MotionEvent event),从而消除slop的影响
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (mCancelable && isShowing() && (event.getAction() == MotionEvent.ACTION_UP && isOutOfBounds(event)
|| event.getAction() == MotionEvent.ACTION_OUTSIDE)) {
cancel();
return true;
}
return super.onTouchEvent(event);
}
//看,没有slop了吧
private boolean isOutOfBounds(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final View decorView = getWindow().getDecorView();
return (x <= 0) || (y <= 0)
|| (x > (decorView.getWidth()))
|| (y > (decorView.getHeight()));
}
}
2、在DialogFragment中重写onCreateDialog(Bundle savedInstanceState)方法,用我们自定义的Dialog来替换原生Dialog:
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
return new WithoutSlopDialog(requireContext(), getTheme());
}
至此,就解决啦。