Catch onTouch event by parent, handle it and pass to children

亡梦爱人 提交于 2019-11-29 01:15:11
JRaymond

My understanding is that it actually goes the other direction. The Child views get their event triggered first (sort of). The root view get's it's dispatchTouchEvent() called, which propagates the event down to the children's onTouchEvent(), and then, depending on whether they return true or false, the parent's onTouchEvent() is called.

The normal solution for intercepting things like this is to override dispatchTouchEvent(MotionEvent ev) in one's activity like so:

@Override
public boolean dispatchTouchEvent (MotionEvent ev) {
  // Do your calcluations
  return super.dispatchTouchEvent(ev);
}

The documentation for this one is here. Note that you can also override that method in any ViewGroup (such as a FrameLayout, etc)

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