这个dialgo/loading 如何可以透传点击事件

init {

window?.let { window ->
    window.setBackgroundDrawableResource(android.R.color.transparent)
    window.attributes?.let { params ->
     
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
        window.attributes = params
    }
}

}

这里,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL将允许对话框外的事件被发送到后面的视图,而WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH将允许对话框在被触摸时接收到外部的触摸事件。这样设置后,用户就可以点击和操作Dialog下面的视图了。

注意,你可能需要处理这个Dialog的点击事件,以便当用户点击到Dialog时,不会触发到后面的视图的点击事件。你可以为Dialog的视图设置一个空的点击事件监听器,像这样:

view.setOnClickListener {
    // Do nothing, just consume the event.
}

这样设置后,当用户点击到Dialog的视图时,事件就会被消费,而不会传递到后面的视图。

完整代码



class Loading(
    val mContext: Context,
) {
    private val flags = listOf(
        "ad",


        )

    lateinit var newFlags: ArrayList
    private val handler = Handler()
    private val runnable = object : Runnable {
        override fun run() {
            mProgressView.loadNextFlag()
            handler.removeCallbacksAndMessages(null)
            handler.postDelayed(this, 300)
        }
    }

    private var currentIndex = 0

    fun showLoadingView() {
        handler.post(runnable)
        mProgressView.showLoading()
    }

    fun hideView() {
        handler.removeCallbacks(runnable)
        try {
            if (!mProgressView.isShowing) return
            mProgressView.dismiss()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private var mProgressView = object : Dialog(mContext) {
        private val flagImageView: ImageView


        init {
            requestWindowFeature(Window.FEATURE_NO_TITLE)

          

            val inflater =
                mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val view = inflater.inflate(R.layout.xxxxx, null)
            flagImageView = view.findViewById(R.id.flagImageView)

            setContentView(view)
            loadNextFlag()

            window?.let { window ->
                window.setBackgroundDrawableResource(android.R.color.transparent)
                window.attributes?.let { params ->
                    params.width = ViewTool.dip2px(mContext, 74f)
                    params.height = ViewTool.dip2px(mContext, 74f)
                    params.gravity = Gravity.CENTER
                    params.dimAmount = 0F
//                    params.y = offsetY
                    params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                    window.attributes = params
                }
            }

            setCancelable(false)
        }

        /**
         *  修改logo默认位置
         *  @param offsetY 偏移距离
         *  如果 是Gravity.BOTTOM 则是距离底部的偏移距离
         *  如果 是Gravity.TOP 则是距离顶部的偏移距离
         *
         */


        fun showLoading() {
            //显示中就不需要再显示了。
            if (isShowing) {
                return
            }


            show()
        }


    }


}

你可能感兴趣的:(android)