禁用特定片段的滑动手势(导航抽屉)

时间:2014-08-19 11:32:38

标签: android navigation-drawer drawerlayout

我想禁用打开导航抽屉的滑动手势,但仅限于特定片段,我的意思是我不想在整个应用程序中禁用它。

我已经阅读了很多问题,似乎其中一个有问题,但我可能不明白我必须做什么: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:我要禁用抽屉的片段。

我很感激任何提示。非常感谢你

更新1:

我试过这个:

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

从我的片段中调用此方法之后:

的onCreate:

Activity activity = getActivity();
    if (activity instanceof MainActivity){
        ((MainActivity) activity).closeDrag();
    } 

2 个答案:

答案 0 :(得分:5)

//Blocked swipe navigation drawer
mDrawer = (DrawerLayout) this.getActivity().findViewById(R.id.drawer_layout);
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

答案 1 :(得分:2)

我使用此代码&它对我有用。

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

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