onOptionsItemSelected无法正常工作

时间:2018-04-08 00:31:24

标签: java android

我正在制作一个滑入屏幕的导航抽屉。我用这段代码做了这个

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/home" android:title="Home"></item>
<item android:id="@+id/change_event" android:title="Change Event"></item>
<item android:id="@+id/FAQ" android:title="FAQ"></item>
<item android:id="@+id/map" android:title="Map"></item>
<item android:id="@+id/Schedule" android:title="Schedule"></item>
</menu>

现在在MainActivity.Java中,我正在尝试使用OnOptionsItemSelected方法在活动之间切换。我正在尝试使用此代码。

@Override
public boolean onOptionsItemSelected(MenuItem item) {


int id = item.getItemId();
   if (id == R.id.home){
       Intent intent = new Intent(MainActivity.this, Home.class);
       startActivity(intent);
   }


   return true;
}

我希望此代码执行的操作是,如果您打开幻灯片菜单并单击主项目,则必须启动活动主页。这不起作用,我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

您必须添加onCreateOptionsMenu

public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your_menu_file_name, menu);
    return true;
}

更新:

NavigationDrawer类

public class MainActivityNew extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;

    public static final String TAG_HOME = "Dashboard";
    public static final String TAG_TEMP = "Temperature";
    public static String CURRENT_TAG = TAG_HOME;

    public static int navItemIndex = 0;

    public Fragment fragment;

    private Fragment sendFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);

        if (savedInstanceState != null) {
            fragment = getSupportFragmentManager().getFragment(savedInstanceState, "myFragmentName");
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                    android.R.anim.fade_out);
            fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
            fragmentTransaction.commit();
        } else {
            navItemIndex = 0;
            CURRENT_TAG = TAG_HOME;
            loadHomeFragment();
        }

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        drawerLayout.setDrawerListener(toggle);
        toggle.syncState();

        navView.setNavigationItemSelectedListener(this);
    }

    private void loadHomeFragment() {
        selectNavMenu();

        if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
            drawerLayout.closeDrawers();
            return;
        } else {
            fragment = getHomeFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                    android.R.anim.fade_out);
            fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
            fragmentTransaction.commit();
            drawerLayout.closeDrawers();
        }
    }

    private Fragment getHomeFragment() {
        switch (navItemIndex) {
            case 0:
                sendFragment = new HomeFragment();
                break;
            case 1:
                sendFragment = new TemperatureFragment();
                break;
            default:
                sendFragment = new TemperatureFragment();
        }
        return sendFragment;
    }

    private void selectNavMenu() {
        navView.getMenu().getItem(navItemIndex).setChecked(true);
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
            return;
        }
        super.onBackPressed();
    }

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

        switch (menuItem.getItemId()) {
            case R.id.home:
                navItemIndex = 0;
                CURRENT_TAG = TAG_HOME;
                break;
            case R.id.temperrature_sensor:
                navItemIndex = 1;
                CURRENT_TAG = TAG_TEMP;
                break;
            default:
                navItemIndex = 0;
        }
        if (menuItem.isChecked()) {
            menuItem.setChecked(false);
        } else {
            menuItem.setChecked(true);
        }
        menuItem.setChecked(true);

        loadHomeFragment();
        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, "myFragmentName", fragment);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onRestoreInstanceState(savedInstanceState, persistentState);
    }
}