Pass touch event from dialog fragment to View righ

2020-03-30 16:22发布

l

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条回答
Viruses.
2楼-- · 2020-03-30 16:41

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

查看更多
登录 后发表回答