选择了两个项目



我在Android中有问题。当我尝试在启动时选择第一个项目,然后选择除第一个项目以外的其他项目,以便我选择了两个项目。这是我的代码。我添加了avaigationview.getmenu()。performidentifierAction(r.id.menu_program,0)以选择第一个项目,但我认为问题来自于此(我不知道)。谢谢

public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    actionBarDrawerToggle = setupActionBarDrawerToggle();
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    setupNavigationViewContent(navigationView);
    if (savedInstanceState == null) {
        navigationView.getMenu().performIdentifierAction(R.id.menu_program, 0);
    }
}
private ActionBarDrawerToggle setupActionBarDrawerToggle() {
    return new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
}
private void setupNavigationViewContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            selectNavigationViewItem(item);
            return true;
        }
    });
}
private void selectNavigationViewItem(MenuItem item) {
    /*if (lastItem == null) {
        lastItem = item;
        item.setChecked(true);
        Log.e("TAG", "selectNavigationViewItem: "+lastItem);
    } else {
        lastItem.setChecked(false);
    }*/
    Fragment fragment = null;
    Class fragmentClass;
    switch (item.getItemId()) {
        case R.id.menu_program:
            fragmentClass = Fragment1.class;
            break;
        case R.id.menu_visitor:
            fragmentClass = Fragment2.class;
            break;
        case R.id.menu_partner:
            fragmentClass = Fragment3.class;
            break;
        case R.id.menu_about:
            fragmentClass = Fragment4.class;
            break;
        case R.id.menu_team:
            fragmentClass = Fragment5.class;
            break;
        case R.id.menu_contact:
            fragmentClass = Fragment6.class;
            break;
        default:
            fragmentClass = Fragment1.class;
    }
    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.frame_layout_content, fragment).commit();
    item.setChecked(true);
    setTitle(item.getTitle());
    drawerLayout.closeDrawers();
}
@Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onPostCreate(savedInstanceState, persistentState);
    actionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    actionBarDrawerToggle.onConfigurationChanged(newConfig);
}

}

进行item.setchecked(true)时,必须取消选中一个。声明benuitem menuitem外部功能并替换您的项目。

if(menuItem != null)
    menuItem.setChecked(false);
menuItem = item;
menuItem.setChecked(true);

相关内容

  • 没有找到相关文章

最新更新