将对话事件从对话框片段传递到右下角的“查看"(在父活动内部) [英] Pass touch event from dialog fragment to View right below (inside the parent activity)

查看:102
本文介绍了将对话事件从对话框片段传递到右下角的“查看"(在父活动内部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您在图像中所见,红色边框是父活动.蓝色的是对话框片段.圆圈表示视图,下面的矩形是说明.我希望将单击圆圈的按钮向下传递给按钮.到目前为止,我已经尝试过 1.在Circle View中覆盖onTouchEvent并返回false 2.在圈子视图上调用setOntouchListener并调用activity.dispatchTouchListener并返回false 3.标记对话框碎片,并在圈子视图上单击可单击/可聚焦"的假值.

以上似乎都不是调用下面按钮的onCLickListener.我可以看到在Activity的onIterceptTouch()中收到了触摸事件.请帮助

活动

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        val viewTreeObserver = button2.viewTreeObserver
        viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {MainFragment.newInstance(button2).show(supportFragmentManager, "dialog")
                button1.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })

        container.setOnClickListener {
            Log.d("Test","Activity clicked")
        }
        button.setOnClickListener {
            Log.d("Test","Button Clicked")
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.d("Test","Activity onTouchEvent")
        return super.onTouchEvent(event)
    }

}

对话框片段

class MainFragment : DialogFragment() {
    companion object {
        fun newInstance(view: View?) : MainFragment {
            val args = Bundle()
            val point = getRippleLocation(getViewCenterLocation(view))
            args.putInt("X", point.x)
            args.putInt("Y", point.y)
            val frag = MainFragment()
            frag.arguments = args
            return frag
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.main_fragment, container, false)

    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.window?.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)
        dialog.window!!.setGravity(Gravity.START or Gravity.TOP)
        point.x = arguments!!.getInt("X")
        point.y = arguments!!.getInt("Y")
        params.x = point.x
        params.y = (point.y - 50)
        dialog.window!!.attributes = params
        return dialog
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        addRippleView(main, 0, 0)
    }

    private fun addCircleView(rootView: ViewGroup, leftMargin: Int, topMargin: Int) {
        val rippleView = BaseCircleView(context, null)
        rippleView.isClickable = false
        rippleView.isFocusable = false
        rippleView.setOnTouchListener(object : View.OnTouchListener{
            override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
                activity!!.dispatchTouchEvent(p1)
                return false
            }
        })
        configureRipple(rippleView)
        context?.resources?.let { resources ->
            rippleView.id = R.id.gather_ripple_view_id
            val params = RelativeLayout.LayoutParams(resources.getDimensionPixelSize(R.dimen.gather_on_boarding_ripple_container_width),
                    resources.getDimensionPixelSize(R.dimen.gather_on_boarding_ripple_container_height))
            params.leftMargin = leftMargin
            params.topMargin = topMargin
            rootView.addView(rippleView, params)
        }
    }

}

解决方案

如果有人仍在努力解决这个问题,这就是解决方法.

dialog?.window?.decorView?.setOnTouchListener { v, event ->
            activity?.dispatchTouchEvent(event)
            false
        }

这将通过触摸事件

As you can see in the image , red border rectangle is the parent activity . Blue one is dialog fragment . The circle is indicating a view and rectangle below is the description. I want the click on circle to be passed down to the button. So far i have tried 1. overriding onTouchEvent in Circle View and return false 2. setOntouchListener on circle view and call activity.dispatchTouchListener and return false 3. mark dialog frgament and circle view clickable/focusable false.

None of the above seems to be calling onCLickListener of the button underneath. I can see touch event being received in Activity's onIterceptTouch() though. Please help

Activity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        val viewTreeObserver = button2.viewTreeObserver
        viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {MainFragment.newInstance(button2).show(supportFragmentManager, "dialog")
                button1.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })

        container.setOnClickListener {
            Log.d("Test","Activity clicked")
        }
        button.setOnClickListener {
            Log.d("Test","Button Clicked")
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.d("Test","Activity onTouchEvent")
        return super.onTouchEvent(event)
    }

}

Dialog Fragment

class MainFragment : DialogFragment() {
    companion object {
        fun newInstance(view: View?) : MainFragment {
            val args = Bundle()
            val point = getRippleLocation(getViewCenterLocation(view))
            args.putInt("X", point.x)
            args.putInt("Y", point.y)
            val frag = MainFragment()
            frag.arguments = args
            return frag
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.main_fragment, container, false)

    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.window?.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)
        dialog.window!!.setGravity(Gravity.START or Gravity.TOP)
        point.x = arguments!!.getInt("X")
        point.y = arguments!!.getInt("Y")
        params.x = point.x
        params.y = (point.y - 50)
        dialog.window!!.attributes = params
        return dialog
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        addRippleView(main, 0, 0)
    }

    private fun addCircleView(rootView: ViewGroup, leftMargin: Int, topMargin: Int) {
        val rippleView = BaseCircleView(context, null)
        rippleView.isClickable = false
        rippleView.isFocusable = false
        rippleView.setOnTouchListener(object : View.OnTouchListener{
            override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
                activity!!.dispatchTouchEvent(p1)
                return false
            }
        })
        configureRipple(rippleView)
        context?.resources?.let { resources ->
            rippleView.id = R.id.gather_ripple_view_id
            val params = RelativeLayout.LayoutParams(resources.getDimensionPixelSize(R.dimen.gather_on_boarding_ripple_container_width),
                    resources.getDimensionPixelSize(R.dimen.gather_on_boarding_ripple_container_height))
            params.leftMargin = leftMargin
            params.topMargin = topMargin
            rootView.addView(rippleView, params)
        }
    }

}

解决方案

If someone is still struggling with this one, this is how is solved it.

dialog?.window?.decorView?.setOnTouchListener { v, event ->
            activity?.dispatchTouchEvent(event)
            false
        }

this will pass touch event through

这篇关于将对话事件从对话框片段传递到右下角的“查看"(在父活动内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆