如何只通过按钮打开SlidingPanelayout?

时间:2014-01-24 13:18:18

标签: android google-maps-android-api-2

我正在使用GoogleMap开发Android应用,我需要在此活动中实现SlidingPanelLayout。 我只想在点击某个按钮时打开SlidingPanelLayout,因为如果我拖动手指 在GoogleMap SlindingPanelLayout出现。因此,如果我仅在单击按钮时修复了打开的SlidingPanelLayout,问题就会得到解决。

当我在地图上拖动时,有什么方法可以阻止SlidingPanelLayout开启吗?

1 个答案:

答案 0 :(得分:3)

如果你真的希望面板只能通过按下按钮打开,你应该创建一个扩展SlidingPaneLayout并覆盖onInterceptTouchEvent()方法的类。

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if(!isOpen()){
        //The map panel is being shown. We don't want the SlidingPaneLayout to handle the MotionEvent.
        return false;
    }
    else{
        //The other panel is being shown... Let the SlidingPaneLayout handle the MotionEvent as normal.
        return super.onInterceptTouchEvent(ev);
    }
}

请记住在代码或布局中使用自定义SlidingPaneLayout类,而不是常规类。此外,显然,您应该放置一个按钮,在某处调用自定义类的openPane()方法。

====

下面的方法不是对问题的回答,而是替代解决方案

现在,如果您想让用户自由使用GoogleMap对象并在屏幕/地图的某个区域发生拖动事件时让SlidingPaneLayout打开,您可以使用以下方法:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    //Get the user touch event position in dp units
    float xTouchPosDp = ev.getX()/getResources().getDisplayMetrics().density;

    if(!isOpen()){
        if(xTouchPosDp < 30){
            //If the panel is closed (map pane being entirely shown) 
            //and the touch event occur on the first 30 horizontal dp's
            //Let the SlidingPaneLayout onTouchEvent() method handle the
            //motion event alone (the GoogleMap object won't receive the event
            //and depending on the movement, the panel will open)
            return true;
        }else{
            //Now, if the panel is closed, but the touch event occur
            //on the rest of the screen, let the GoogleMap object handle
            //the motion event.
            return false;
        }
    }
    else{
        //If the panel is opened, let the SlidingPaneLayout handle the 
        //motion event normally.
        return super.onInterceptTouchEvent(ev);
    }
}

同样,请记住在代码/布局中使用自定义SlidingPaneLayout类。

此解决方案的问题在于,如果两个面板都打开(它们一起适合整个屏幕),您将无法横向移动地图。