如何防止自定义视图将触摸事件传递给父 viewpager

How to prevent a custom view passing touch events to parent viewpager

我在 viewpager 中有几个自定义视图,我发现 viewpager's ontouch 事件将与自定义视图的 ontouchevent 发生冲突,这将导致因为我无法按预期拖动自定义视图。为了解决这个问题,我尝试将自定义视图的 ontouchevent 设置为 return true 但 viewpagertouchevent 仍然会被触发。如何防止 viewpager 被处理 ontouchevent?非常感谢任何帮助!

ViewPager 使用 onInterceptTouchEvent() 在其子项之前获取触摸。看一下 docs 并根据需要覆盖 Pager 的 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. 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.