Pass touch event from dialog fragment to View right below (inside the parent activity)

假装没事ソ 提交于 2019-12-19 11:21:11

问题


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)
        }
    }

}

回答1:


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



来源:https://stackoverflow.com/questions/58891692/pass-touch-event-from-dialog-fragment-to-view-right-below-inside-the-parent-act

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!