【Android】修复部分系统拖拽窗口导致弹窗消失的问题

问题

PopupWindow内部对外部事件进行了过滤,如果触发外部事件,会自动关闭弹窗

有些处理得不够好的系统,拖拽边缘时会被误认为是Outside事件来处理,这样就导致了弹窗关闭

方法

对TouchEvent进行拦截,过滤混淆事件

代码
kotlin 复制代码
    override fun setContentView(view: View) {
        super.setContentView(view)
        interceptOutsideEvent()
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun interceptOutsideEvent() {
        setTouchInterceptor { v, e ->
            val decorView = contentView.rootView
            if ((e.action == MotionEvent.ACTION_DOWN)) {
                val outside = e.x < 0 || e.x >= decorView.measuredWidth || e.y < 0 || e.y >= decorView.measuredHeight
                if (outside) {
                    ToastUtils.show("outside")
                    return@setTouchInterceptor true
                }
            } else if (e.action == MotionEvent.ACTION_OUTSIDE) {
                ToastUtils.show("outside")
                return@setTouchInterceptor true
            }
            return@setTouchInterceptor false
        }
    }
相关推荐
baidu_2474386122 分钟前
Android 35适配
android
mmsx1 小时前
osmdroid 踩坑清单:大级别崩溃/路径消失/低内存卡顿/范围缩放不准
android·源码·地图·osmdroid
gf13211111 小时前
【python_回复邮件】
android·java·python
段一凡-华北理工大学2 小时前
高炉智能布料技术与炉料分布优化~专栏简介与目录
android·人工智能·高炉智能化·高炉布料·高炉布料分布·高炉布料参数优化·高炉布料矩阵
hunterandroid2 小时前
Android 多渠道打包与 Gradle 构建优化实战
android·前端·kotlin
小白羊丨2 小时前
异步任务创建接口如何幂等?
android·数据库
用户0934077735143 小时前
HarmonyOS WPS Open SDK 实践:把水印与修订收成打开策略层
android·typescript·harmonyos
sumatch3 小时前
ESP32
android
pengyu3 小时前
【Kotlin 协程修仙录 · 大乘境 · 后阶】 | 死锁天劫:协程同步原语与 ThreadLocal 迁移之道
android·kotlin