Carl Carl - 14 days ago 8
Android Question

Disable swipe gesture (navigation drawer) from specific fragment

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();
}

Answer

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.