java - 我正在尝试使用抽屉导航而不是 fragment 在 Activity 之间切换

标签 java android uinavigationbar navigation-drawer

下面是我的应用程序代码。我尝试过这方面的工作,但没有遇到任何对我成功的事情。不变成 fragment 我能做什么。

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

        if (id == R.id.my_account) {

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

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

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

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

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

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

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

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

        } else if (id == nav_fanzone) {

        }

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

最佳答案

将您的 onNavigationItemSelected() 更改为:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        switch(item.getItemId()){
             case R.id.my_account:
               //Do code here
              break;
             case R.id.nav_news:
               //Do code here
             break;
             case R.id.nav_live:
              //Do code here
             break;
             case R.id.nav_media:
              //Do code here
             break;
             case R.id.nav_calendar:
              //Do code here
             break;
             case R.id.nav_results:
              //Do code here
             break;
             case R.id.nav_about:
              //Do code here
             break;
             case R.id.nav_shop:
              //Do code here
             break;
             case R.id.nav_social_media:
              //Do code here
             break;
             case R.id.nav_fanzone:
              //Do code here
             break;

}

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

对于更改 Activity ,只需在所有switch cases中添加:

Intent intent=new Intent(your_activity.this,your_next_activity.class);
startActivity(intent);

关于java - 我正在尝试使用抽屉导航而不是 fragment 在 Activity 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45289035/

相关文章:

ios - 更改 UINavigationBar 属性的最佳位置?

ios - 为什么我看不到我的导航栏?

java - jcombobox 用 hashmap 填充

java - Spring @ContextConfiguration 中加载位置的顺序

android - Dagger 2 错误 : dependency "cannot be provided without an @Inject constructor" while it actually annotated with @Inject

android - 传入短信的编码

Android WebClient,通过WebResourceResponse 返回图像资源 - 不显示图像

java - noClassDefFounderror 安卓

java - 如何获取模板类的类?

ios - 如何完全覆盖导航栏的后退按钮操作?