Android的,禁止刷卡与SlidingPaneLayout互动互动、Android、SlidingPaneLayout

2023-09-04 03:07:03 作者:兜兜里有根棒棒糖

我需要禁用刷卡打开/关闭SlidingPaneLayout,因为我的主要观点是一张地图。 我会打开/关闭它使用一个按钮。

I need to disable swiping to open/close SlidingPaneLayout because my main view is a map. I'll open/close it using a button.

推荐答案

子类 SlidingPaneLayout ,覆盖 onInterceptTouchEvent()并使其总是返回。这将prevent SlidingPaneLayout 从处理触摸事件。

Subclass SlidingPaneLayout, override onInterceptTouchEvent() and make it always return false. This will prevent SlidingPaneLayout from handling touch events.

请参阅 http://developer.android.com/training/gestures/viewgroup.html 了解更多信息。

更新: 如果触摸事件不是由子视图(S)处理,在 SlidingPaneLayout 的onTouchEvent()方法将不过叫。要完全禁用处理触摸事件还覆盖的onTouchEvent()和总是返回

Update: If touch events are not handled by the child view(s), the SlidingPaneLayout's onTouchEvent() method will be called nevertheless. To completely disable handling touch events also override onTouchEvent() and always return false.

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return false;
}

注意:这种方法的缺点是,触摸事件仍然可以分派到第二子视图时,它只是部分可见。所以,你可能想看看其他的答案。

Note: The downside of this approach is that touch events still get dispatched to the second child view when it's only partly visible. So you might want to check out the other answers.