单击抽屉菜单项时如何更改活动?

时间:2017-04-05 13:01:35

标签: android

我有一个由Android Studio提供的抽屉菜单。

我的菜单代码:

        //Menü kodları//
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setItemIconTintList(null);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menu_fbdown) {

    } else if (id == R.id.menu_instadown) {

    } else if (id == R.id.menu_twitdown) {

    } else if (id == R.id.menu_privacy) {

    } else if (id == R.id.menu_about) {

    } else if (id == R.id.menu_contact) {

    }

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

}

我想点击此菜单。我厌倦了这段代码:

     Intent i = new Intent(FBDownActivity.this, FBDownActivity.class);
        startActivity(i);

像这样:

 @Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menu_fbdown) {
        Intent i = new Intent(FBDownActivity.this, FBDownActivity.class);
        startActivity(i);
    } else if (id == R.id.menu_instadown) {
        Intent i = new Intent(FBDownActivity.this, FBDownActivity.class);
        startActivity(i);
    } else if (id == R.id.menu_twitdown) {

    } else if (id == R.id.menu_privacy) {

    } else if (id == R.id.menu_about) {

    } else if (id == R.id.menu_contact) {

    }

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

但它没有用。仍然无法点击项目。 我该怎么办?

感谢。

1 个答案:

答案 0 :(得分:0)

您正在尝试开始相同的活动。更改下面的代码:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menu_fbdown) {
        Intent i = new Intent(YourCurrentActivty.this, ActivityToOpen.class);
        startActivity(i);
    } else if (id == R.id.menu_instadown) {
        Intent i = new Intent(YourCurrentActivty.this, ActivityToOpen.class);
        startActivity(i);
    } else if (id == R.id.menu_twitdown) {

    } else if (id == R.id.menu_privacy) {

    } else if (id == R.id.menu_about) {

    } else if (id == R.id.menu_contact) {

    }

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