处理onNavigationItemSelected中的可点击项

时间:2016-05-30 15:43:03

标签: android android-intent onclick android-navigation-drawer

我有导航抽屉布局,我想点击该项目然后导航到另一个意图, 我尝试过这个,但它不起作用。

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_manage) {
        //Toast.makeText(getApplicationContext(),"Hallo berghaasiALSj ia",Toast.LENGTH_SHORT);
        startActivity(new Intent(this, UtamaClass.class));
        return true;
    } else if (id == R.id.nav_share) {
        Intent intent = new Intent(MainActivity.this, UtamaClass.class);
        startActivity(intent);
    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

    return true;
}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

尝试使用setNavigationItemSelectedListener(OnNavigationItemSelectedListener listener)

NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                   //your code
                }
             }

        });