Android:拦截从父视图到子视图的触摸手势处理



所以我有以下视图结构:

  • 线性布局
    • 水平滚动视图
    • 其他子视图

父线性布局是可点击的,有一个自定义选择器(按下时改变颜色)。我希望能够触摸LinearLayout中的HorizontalScrollView,并且仍然处理LinearLayout中的触摸,只要它不是滚动运动。如果我做一个滚动动作,那么水平滚动视图应该拦截手势并取消线性布局的触摸。基本上,我希望能够从子视图而不是父视图拦截手势,这是标准。

我尝试通过创建执行以下操作的扩展类来手动处理 MotionEvent:

线性布局

public override bool OnInterceptTouchEvent(MotionEvent ev)
{
// Handle the motion event even if a child returned true for OnTouchEvent
base.OnTouchEvent(ev);
return base.OnInterceptTouchEvent(ev);
}

水平滚动视图

public override bool OnTouchEvent(MotionEvent e)
{
if (e.Action == MotionEventActions.Down)
{
_intialXPos = e.GetX();
}
if (e.Action == MotionEventActions.Move)
{
float xDifference = Math.Abs(e.GetX() - _intialXPos);
if (xDifference > _touchSlop)
{
// Prevent the parent OnInterceptTouchEvent from being called, thus it will no longer be able to handle motion events for this gesture
Parent.RequestDisallowInterceptTouchEvent(true);
}
}
return base.OnTouchEvent(e);
}

这几乎奏效了。当我触摸水平滚动视图时,线性布局显示按下状态 UI,并在单击完成后激活。如果我触摸并滚动水平滚动视图,则滚动有效。当我放开滚动时,线性布局的单击处理程序不会触发,因为它被拦截了。但问题是,在我开始滚动之前,LinearLayout 会更改为按下状态,即使在手势完成后它也不会重置。在我尝试手动取消线性布局手势的额外尝试中,我一直遇到其他问题。此外,LinearyLayout 内部还有其他按钮,单击这些按钮时不应允许父 LinearLayout 显示按下状态。有什么建议吗?是否有固定的模式来拦截孩子的触摸事件?我敢肯定,如果两个类都相互了解,这是可能的,但我试图避免耦合它们。

以下适用于所有情况:

可拦截线性布局

public override bool DispatchTouchEvent(MotionEvent e)
{
bool dispatched = base.DispatchTouchEvent(e);
// Handle the motion event even if a child returns true in OnTouchEvent
// The MotionEvent may have been canceled by the child view
base.OnTouchEvent(e);
return dispatched;
}
public override bool OnTouchEvent(MotionEvent e)
{
// We are calling OnTouchEvent manually, if OnTouchEvent propagates back to this layout do nothing as it was already handled.
return true;
}

拦截能力儿童视图

public override bool OnTouchEvent(MotionEvent e)
{
bool handledTouch = base.OnTouchEvent(e);
if ([Meets Condition to Intercept Gesture])
{
// If we are inside an interceptable viewgroup, intercept the motionevent by sending the cancel action to the parent
e.Action = MotionEventActions.Cancel;
}
return handledTouch;
}

最新更新