DrawerLayout 阻止调用 MainActivity.onTouchEvent()

owe*_*owe 5 android touch-event drawerlayout

我有一个应用程序可以覆盖 的onTouchEvent(MotionEvent ev)MainActivity确定Two-Finger-SwipePich-Open/ Pinch-Close。一切正常,直到我将其添加到应用程序中(就像创建导航抽屉DrawerLayout中所述)。问题:阻止在 MainActivity 中调用。DrawerLayoutonTouchEvent()

我开始编写CustomDrawerLayout, 并尝试重写DrawerLayout方法onInterceptTouch()onTouchEvent()

TouchEvent(我发现)将其传输到 MainActivity 的唯一方法:

    // onTouchEvent of CustomDrawerLayout 
    @Override
    public boolean onTouchEvent(MotionEvent ev){
        // super.onTouchEvent(ev); // prevent transmission of TouchEvent
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

这里的问题是抽屉无法正确打开。抽屉卡住了,如这篇文章中所述: DrawerLayout getting 卡在 swipe 上

是否可以将 传输TouchEvent到 MainActivity 来处理 MultiTouchDetection?或者我必须处理这个问题CustomDrawerLayout

更新1

首先,我不得不说,如果我从左边缘滑动,抽屉只会卡住。通过单击ActionBar抽屉中的抽屉图标,效果很好。TouchEvent使用以下代码传输作品。但前提是抽屉打开了!否则Activity.onTouchEvent不叫!

    // onTouchEvent of CustomDrawerLayout 
    @Override
    public boolean onTouchEvent(MotionEvent ev){
        // super.onTouchEvent(ev); // still prevents transmission of TouchEvent
        activity.onTouchEvent(ev);
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

通过从边缘滑动打开抽屉(->抽屉卡住),我得到了一个非常奇怪的行为:

  • 只是 DrawerIcon 可以关闭“卡住的抽屉”(->我不期望有什么不同,因为我覆盖了CustomDrawerLayout.onTouchEvent
  • 如果我通过 DrawersIcon 关闭卡住的抽屉,CustomDrawerLayout.onTouchEvent仍然会调用

真奇怪!为什么Activity.onTouchEvent()没有被调用?如何防止抽屉卡住?

更新2

现在我覆盖CustomDrawerLayout.onInterceptTouch()

@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
        return true;
}
Run Code Online (Sandbox Code Playgroud)

这会导致抽屉无法通过从边缘滑动来打开 -> 只有 DrawersIcon 可以打开和关闭抽屉。但现在TouchEvent总是传输到活动(->按预期工作)。

但我真正想要的是能够通过从边缘滑动来打开抽屉拥有我的多手势检测器。这可能吗?

小智 3

更新有点晚,但在遇到这个问题几天后,最适合我的解决方案是创建 CustomDrawerLayout。然后,将构造函数中的 Context 转换为 Activity,从 onInterceptTouchEvent 调用 Activity onTouchEvent。

@Override public boolean onInterceptTouchEvent( MotionEvent ev )
{
    getActivity().onTouchEvent( ev );
    return super.onInterceptTouchEvent( ev );
}
Run Code Online (Sandbox Code Playgroud)

我发现这段代码是一个蹩脚的黑客......但对我有用。祝你好运!