在android中为Navigation抽屉创建自定义触摸操作

时间:2015-04-27 03:26:13

标签: android navigation-drawer

android中的导航抽屉有3个行为

  - Always opened, and can be close only programming using LOCK_MODE_LOCKED_OPEN.
  - Always closed, and can be open only  programming LOCK_MODE_LOCKED_CLOSED.
  - User can open and close the drawer. using LOCK_MODE_UNLOCKED

如果用户在抽屉打开时点击抽屉侧面,第三个将自动关闭。虽然第一个将始终保持开放。

I need to have custom one that user can open and close the drawer, and when he clicks outside the drawer it will remains open. 

我正在考虑重写onInterceptTouchEvent。

有人做了一些similer吗?

/ *********交锋*********** 我扩展了DrawerLayout,它还不确定是否100%

public class CustomDrawerLayout extends DrawerLayout {
  Context ccontext;
  public CustomDrawerLayout(Context context) {
      this(context, null);
  }

public CustomDrawerLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle)      {
 super(context,attrs,defStyle);
    this.ccontext = context;
}

//@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();


    switch (action & MotionEventCompat.ACTION_MASK) {


        case MotionEvent.ACTION_UP: {
            final float x = ev.getX();
            final float y = ev.getY();
            boolean peekingOnly = true;
            final View touchedView = findTopChildUnder((int) x, (int) y);
            if (touchedView != null && touchedView instanceof FrameLayout) {
                return true;
            }
            break;
        }
    }
    return super.onTouchEvent(ev);

}

public View findTopChildUnder(int x, int y) {
    final int childCount = this.getChildCount();
    for (int i = childCount - 1; i >= 0; i--) {
        final View child = this.getChildAt(i);
        if (x >= child.getLeft() && x < child.getRight() &&
                y >= child.getTop() && y < child.getBottom()) {
            return child;
        }
    }
    return null;
}
}

1 个答案:

答案 0 :(得分:0)

您也知道,这样做违反Android准则。

https://developer.android.com/design/patterns/navigation-drawer.html

当导航抽屉展开时,用户可以通过以下四种方式之一解除它:触摸外部内容导航抽屉向左滑动屏幕上的任何位置(包括右侧边缘滑动)触摸操作栏中的应用图标/标题