抽屉图标未响应单击

时间:2019-04-03 08:10:55

标签: android drawerlayout

我有一个DrawerToggle来显示DrawerLayout。我是这样做的:

mDrawerLayout = findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,0, 0) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            invalidateOptionsMenu();
        }
    };
    mDrawerToggle.setDrawerIndicatorEnabled(false);
    Drawable drawable= ResourcesCompat.getDrawable(getResources(),R.drawable.menu,getTheme());
    mDrawerToggle.setHomeAsUpIndicator(drawable);
    mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("David", "onClick");
            if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
                mDrawerLayout.closeDrawer(GravityCompat.START);
            } else {
                mDrawerLayout.openDrawer(GravityCompat.START);
            }
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

但是,单击图标时我没有得到响应。我该如何解决?

1 个答案:

答案 0 :(得分:1)

您尚未添加代码来打开抽屉。在活动类中添加以下代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            drawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

关注:https://developer.android.com/training/implementing-navigation/nav-drawer