【问题标题】:activity in fragment menu片段菜单中的活动
【发布时间】:2016-01-08 15:38:08
【问题描述】:

我是应用程序开发的菜鸟,但我正在努力! 我已经构建了一个应用程序的基础,首先在 android studio 中尝试。 构建一个包含片段的菜单。 但是,我希望将一个菜单项链接到一项活动(因为我希望它看起来与其他项不同。) 尝试了很多,但它不起作用。 你能帮帮我吗?

这是我的菜单代码:

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

    int id = item.getItemId();

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

        MainFragment fragment = new MainFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

    } else if (id == R.id.nav_gallery) {
        Zakkaartjes fragment = new Zakkaartjes();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

    } else if (id == R.id.nav_slideshow) {
        Rekenen fragment = new Rekenen();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

    } else if (id == R.id.nav_manage) {
         //THIS IS WHERE I WOULD LIKE TO HAVE A LINK TO MY "notities activity"

    } else if (id == R.id.nav_share) {
        Protocollen fragment = new Protocollen();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();


    } else if (id == R.id.nav_send) {
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                "mailto","info@verpleegkundigzakboek.nl", null));
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email van verpleegkundig zakboek app");
        startActivity(Intent.createChooser(emailIntent, "Verzend e-mail"));

    }

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

制作 Notities.java(其中包含其他布局)并制作 activity_notities.xml 希望有人能帮助我吗?!

【问题讨论】:

    标签: android android-activity fragment android-menu


    【解决方案1】:

    如果我理解正确,这应该可以:

    else if (id == R.id.nav_manage) {
        Intent intent = new Intent(this, YourNextActivity.class);
        startActivity(intent);
        finish();
    }
    

    另外,您可以考虑使用switch 而不是很多else if

    public boolean onNavigationItemSelected(MenuItem item) {
    
        switch (item.getId()){
            case R.id.nav_camera:
                //your code
                break;
            case R.id.nav_gallery:
                //your code
                break;
            // add case blocks for each id you want to manage
        }
    }
    

    更多关于switchhere

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      相关资源
      最近更新 更多