Open NavigationView on Right Swipe from everywhere on the layout

痴心易碎 提交于 2019-12-11 11:17:26

问题


I would like to open the navigationview on a right swipe, no matter where the right swipe is. Default you have to make the right swipe quite at the side, but I want to swipe for example from the middle to open the navigationview


回答1:


you could either increase the swipe edge of the drawer using something like this

public static void increaseSwipeEdgeOfDrawer(DrawerLayout mDlSearchDrawer) {
        try {

            Field mDragger = mDlSearchDrawer.getClass().getDeclaredField(
                    "mRightDragger");//mRightDragger or mLeftDragger based on Drawer Gravity
            mDragger.setAccessible(true);
            ViewDragHelper draggerObj = (ViewDragHelper) mDragger
                    .get(mDlSearchDrawer);

            Field mEdgeSize = draggerObj.getClass().getDeclaredField(
                    "mEdgeSize");
            mEdgeSize.setAccessible(true);
            int edge = mEdgeSize.getInt(draggerObj);


            mEdgeSize.setInt(draggerObj, <size of the edge here>);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

or you could give keep a view on top that matches the size of the drawer layout, assign on on tough listener to it to detect a swipe and call DrawerLayout#openDrawer(android.view.View) on it




回答2:


You need to @override onTouchEvent(MotionEvent event) and can detect right swipe in your drawer activity, then open drawer in that.

@Override
public boolean onTouchEvent(MotionEvent event)
{     
    switch(event.getAction())
    {
      case MotionEvent.ACTION_DOWN:
          x1 = event.getX();                         
      break;         
      case MotionEvent.ACTION_UP:
          x2 = event.getX();
          float deltaX = x2 - x1;

          if (Math.abs(deltaX) > MIN_DISTANCE)
          {
              // Left to Right swipe action
              if (x2 > x1)
              {
                  Toast.makeText(this, "Left to Right swipe [Next]", Toast.LENGTH_SHORT).show ();   
                  drawer.openDrawer();     //OPEN YOUR DRAWER HERE              
              }

          }
          else
          {
              // consider as something else - a screen tap for example
          }                          
      break;   
    }           
    return super.onTouchEvent(event);       
}


来源:https://stackoverflow.com/questions/34877797/open-navigationview-on-right-swipe-from-everywhere-on-the-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!