[英]How to prevent a custom view passing touch events to parent viewpager
I have a couple of custom views in a viewpager
and I find the viewpager's
ontouch
event will conflict with the ontouchevent
of the custom view which will result in that I can not drag the custom view around as expected. 我在
viewpager
有几个自定义视图,我发现viewpager's
ontouch
事件将与自定义视图的ontouchevent
冲突,这将导致我无法按预期拖动自定义视图。 To resolve this problem I try to set the ontouchevent
of the custom view to return true but the touchevent
of viewpager
still gets triggered. 为了解决这个问题,我尝试将自定义视图的
ontouchevent
设置为true,但仍然会触发viewpager
的touchevent
。 How Can I prevent the viewpager
from being processing ontouchevent
? 如何防止
viewpager
在ontouchevent
进行处理? Any help is much appreciated! 任何帮助深表感谢!
The ViewPager uses onInterceptTouchEvent()
to get the touches before its children. ViewPager使用
onInterceptTouchEvent()
来获取其子项之前的触摸。 Take a look at the docs and overwrite onInterceptTouchEvent()
of the Pager as you like. 查看文档并根据需要覆盖
onInterceptTouchEvent()
。
The onInterceptTouchEvent() method is called whenever a touch event is detected on the surface of a ViewGroup, including on the surface of its children.
只要在ViewGroup的表面上检测到触摸事件(包括在其子表面上),就会调用onInterceptTouchEvent()方法。 If onInterceptTouchEvent() returns true, the MotionEvent is intercepted, meaning it will be not be passed on to the child, but rather to the onTouchEvent() method of the parent.
如果onInterceptTouchEvent()返回true,则截取MotionEvent,这意味着它不会传递给子节点,而是传递给父节点的onTouchEvent()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.