【问题标题】:make main content area active in DrawerLayout使主要内容区域在 DrawerLayout 中处于活动状态
【发布时间】:2013-09-11 13:51:56
【问题描述】:

如何在抽屉打开时激活主要内容区域。目前,如果点击主要内容区域,抽屉将关闭然后触摸事件将被丢弃,我想捕捉该触摸事件并将其传递给主要内容区域。

【问题讨论】:

    标签: android navigation-drawer drawerlayout


    【解决方案1】:

    您需要创建自定义 DrawerLayout 和 @overide onInterceptTouchEvent。然后您可以创建一些方法来注册抽屉视图(如setDrawerViewWithoutIntercepting),当它们打开时,可以使用(单击)主要内容。你应该在你的活动/片段中调用这个方法,这个 DrawerLayout 在哪里使用。

    OnInterceptTouchEvent 将返回 false 以防已注册的抽屉视图打开并且触摸位于抽屉视图区域之外。这意味着所有功能都保持不变(例如使用滑动关闭抽屉视图等)

    这只是一个例子,有待改进,希望对你有所帮助。

    private View currentDrawerView;
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean result = super.onInterceptTouchEvent(event);
    
        if (currentDrawerView != null && isDrawerOpen(currentDrawerView)) {
            DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) currentDrawerView.getLayoutParams();
    
            if (layoutParams.gravity == Gravity.RIGHT) {
                if (event.getX() < currentDrawerView.getX()) {
                    result = false;
                }
            }
            else if (layoutParams.gravity == Gravity.LEFT) {
                if (event.getX() > currentDrawerView.getX() + currentDrawerView.getWidth()) {
                    result = false;
                }
            }
            // ......
    
        };
        return result;
    }
    
    public void setDrawerViewWithoutIntercepting(View view) {
        this.currentDrawerView = view;
    }
    

    【讨论】:

      【解决方案2】:

      我用临时解决方案解决了我的问题,不是那么优雅,但可以完成一段时间的工作,可能包含副作用。我总是在 onInterceptTouchEvent() 调用中返回false,并且每次都处于活动状态。

      public class CustomDrawerLayout extends DrawerLayout {
      
          public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
          }
      
          public CustomDrawerLayout(Context context, AttributeSet attrs) {
              super(context, attrs);
          }
      
          public CustomDrawerLayout(Context context) {
              super(context);
          }
      
          @Override
          public boolean onInterceptTouchEvent(MotionEvent ev) {
              // always false and event will be send to each view
              return false;
          }   
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        相关资源
        最近更新 更多