工具栏上的按钮不会打开导航抽屉

Button on ToolBar won't open the Navigation Drawer

我创建了一个包含导航抽屉的基础 Activity。

抽屉将在幻灯片上打开,但绝不会通过工具栏上的导航按钮打开。我已经坚持了很长一段时间,在创建这个基础之前我已经开始工作 class 而且我认为我没有在逻辑上改变任何东西。

我知道以前有人问过这个问题,但我已经浏览了所有其他类似的帖子,但未能解决。

提前致谢!

public class DrawerActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


protected DrawerLayout          drawerLayout;
private   ActionBarDrawerToggle toggle;
private   Toolbar               toolbar;
private   NavigationView        navigationView;
private   boolean               enableToolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    enableToolbar  =  true;

}


@Override
public void setContentView(int layoutResID){
    super.setContentView(R.layout.activity_drawer);
    setToolbar();
    setDrawerLayout();
    setNavigationDrawer();
    setToolbarOnClickListener();
    if(!useToolbar())
        toolbar.setVisibility(View.GONE);
    inflateToContentFrame(layoutResID);
}


public void setUsesToolbar(boolean _enable){
    enableToolbar = _enable;
}


private boolean useToolbar(){
    return enableToolbar;
}


private void setToolbar(){
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}


private void setDrawerLayout(){
    drawerLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_drawer, null);
    toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close){

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            DrawerActivity.this.invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            DrawerActivity.this.invalidateOptionsMenu();
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            DrawerActivity.this.toolbar.setAlpha(1 - slideOffset / 2);
        }
    };

    toggle.setDrawerIndicatorEnabled(true);
    drawerLayout.addDrawerListener(toggle);
    drawerLayout.post(new Runnable() {
        @Override
        public void run() {
            toggle.syncState();
        }
    });

}

private void setNavigationDrawer(){
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}


private void setToolbarOnClickListener(){
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DrawerActivity.this.drawerLayout.openDrawer(GravityCompat.START);
        }
    });
}


private void inflateToContentFrame(int layoutResID){
    FrameLayout content = (FrameLayout) findViewById(R.id.content_frame);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(layoutResID, content, true);
}


@Override
public void onBackPressed() {
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.drawer, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();
    Intent _intent;
    if (id == R.id.nav_classes) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_appointments) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_clients) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_payments) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if(id == R.id.nav_Settings){
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_share) {
        _intent = new Intent(this, AppointmentsActivity.class);
    } else if (id == R.id.nav_send) {
        _intent = new Intent(this, AppointmentsActivity.class);
    }else{
        _intent = new Intent(this, AppointmentsActivity.class);
    }

    drawerLayout.closeDrawer(GravityCompat.START);
    startActivity(_intent);
    return true;
}



}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>


<include
    layout="@layout/app_bar_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />



<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_drawer"
    app:menu="@menu/activity_drawer_drawer"
    />




</android.support.v4.widget.DrawerLayout>

您用来设置 Toggle 的 DrawerLayout 实例不是屏幕上的实例。在 setDrawerLayout() 方法中,您正在膨胀一个从未使用过的新布局。

而不是在那里膨胀,而是使用 findViewById() 来获取创建并添加到 super.setContentView() 调用中的 ActivityDrawerLayout 实例。

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

此外,您可以省略 toolbar.setNavigationOnClickListener() 调用,因为 ActionBarDrawerToggle 将自己设置一个侦听器,并在内部处理抽屉的打开和关闭。