启动时禁用导航抽屉

时间:2014-09-21 17:06:09

标签: android navigation-drawer

我希望我的导航抽屉在加载应用程序时被禁用,如果用户执行某些特定任务,则会启用它。

简而言之,有一种方法可以禁用导航抽屉的切换按钮,并根据用户操作再次启用它。

编辑: 我已将我的活动更新如下;

public class MainActivity extends Activity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    private NavigationDrawerFragment mNavigationDrawerFragment;

    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        DrawerLayout navigationDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                navigationDrawerLayout);

        navigationDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }
}

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        if (item.getItemId() == android.R.id.home) {

        }
        return super.onOptionsItemSelected(item);
    }
    ...
}

但是当应用程序启动时,我可以打开导航抽屉,这是我不想要的

2 个答案:

答案 0 :(得分:4)

您只需使用DrawerLayout DrawerLayout方法锁定和解锁setDrawerLockMode()

因此,要将DrawerLayout锁定在关闭模式,请使用:

drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

如果您希望将其锁定在开放模式,请使用drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);

最后,当您要解锁DrawerLayout时,请使用:

drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

答案 1 :(得分:0)

write condtion in onoptionsitemselected and block it


@Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
if(yourconditionsatisfied)
{
            if (mDrawerLayout.isDrawerOpen(mDrawerLinearLayout)) {
                mDrawerLayout.closeDrawer(mDrawerLinearLayout);
            } else {
                mDrawerLayout.openDrawer(mDrawerLinearLayout);
            }
    }
        }
        return super.onOptionsItemSelected(item);
    }