简体   繁体   中英

Button on ToolBar won't open the Navigation Drawer

I have created a Base Activity that contains a navigation drawer.

The drawer will open on slide, but never through the navigation button on the toolbar. I've been stuck on this for quite a while, and I had it working prior to creating this base class and I don't think I've changed anything logically.

I know this question has been asked before, but I've gone through all other similar posts and have not been able to solve it.

Thanks in advance!

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>

The DrawerLayout instance you're setting up the Toggle with is not the instance that's on-screen. In the setDrawerLayout() method, you're inflating a new layout that's never used.

Instead of inflating there, use findViewById() to get the DrawerLayout instance that's created and added to the Activity in the super.setContentView() call.

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

Also, you can omit the toolbar.setNavigationOnClickListener() call, as the ActionBarDrawerToggle will set a listener itself, and handle opening and closing the drawer internally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM