2

I would like to disable the swipe gesture that opens the navigation drawer but only from an specific fragment, I mean I don't want to disable it from whole application.

I have read many questions and it seems one of them works, but I probably dont understand exactly what I have to do: this

I have tried this:

MainActivity, onCreate:

mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, R.layout.fragment_my_favourite);

R.layout.fragment_my_favourite: fragment where I want to disable the drawer.

I would appreciate any hint. Thank you a lot

UPDATE 1:

I have tried this:

MainActivity:

private ViewDragHelper draggerObj;
private Field mDragger;
private Field mEdgeSize;
private int edge;
private DrawerLayout mDrawer;

public void closeDrag()
{       
    try 
    {
       mDragger = mDrawer.getClass().getDeclaredField("mLeftDragger");
    }
    catch (NoSuchFieldException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    mDragger.setAccessible(true);

    try 
    {
        draggerObj = (ViewDragHelper) mDragger.get(mDrawer);
    }
    catch (IllegalArgumentException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IllegalAccessException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try 
    {
        mEdgeSize = draggerObj.getClass().getDeclaredField("mEdgeSize");
    } 
    catch (NoSuchFieldException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    mEdgeSize.setAccessible(true);

    try 
    {
      edge = mEdgeSize.getInt(draggerObj);
    } 
    catch (IllegalArgumentException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IllegalAccessException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try 
    {
        mEdgeSize.setInt(draggerObj, edge * 0);
    }
    catch (IllegalArgumentException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IllegalAccessException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

And after I call this method from my fragment:

onCreate:

Activity activity = getActivity();
    if (activity instanceof MainActivity){
        ((MainActivity) activity).closeDrag();
    } 
Community
  • 1
  • 1
Carl
  • 111
  • 1
  • 12

2 Answers2

5
//Blocked swipe navigation drawer
mDrawer = (DrawerLayout) this.getActivity().findViewById(R.id.drawer_layout);
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Leonardo Dias
  • 71
  • 1
  • 6
  • Code dumps without explanation are rarely useful. Please consider adding some context to your answer. – ChrisGPT was on strike Oct 22 '14 at 13:21
  • Your answer has easy method but it would be good if you also add the gravity or from where your drawer will get open like Gravity.LEFT or Gravity.RIGHT and it also add more stability. Check Here the official guide https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html#setDrawerLockMode(int, int) – Chintan Desai Jun 08 '16 at 12:19
2

I use this code & it works for me.

public void closeDrag()
    {
        try 
        {
            mDragger = drawerLayout.getClass().getDeclaredField("mLeftDragger");
        }
        catch (NoSuchFieldException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mDragger.setAccessible(true);

        try 
        {
            draggerObj = (ViewDragHelper) mDragger.get(drawerLayout);
        }
        catch (IllegalArgumentException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalAccessException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try 
        {
            mEdgeSize = draggerObj.getClass().getDeclaredField("mEdgeSize");
        } 
        catch (NoSuchFieldException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mEdgeSize.setAccessible(true);

        try 
        {
            edge = mEdgeSize.getInt(draggerObj);
        } 
        catch (IllegalArgumentException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalAccessException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try 
        {
            mEdgeSize.setInt(draggerObj, edge * 0);
        }
        catch (IllegalArgumentException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalAccessException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Note: it will allow to open/close drawer from Actionbar button but will disable finger swipe.