Android风格选择了两个项目

时间:2017-02-18 12:05:09

标签: android view menu navigation drawer

我在android中遇到了问题。当我尝试在启动时选择第一个项目,并选择除第一个项目以外的其他项目,因此我选择了两个项目。这是我的代码。我已经添加了navigationView.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);
}

}

1 个答案:

答案 0 :(得分:0)

当您执行item.setChecked(true)时,您必须取消选中上一个。 在函数外部声明一个MenuItem menuItem,并使用以下代码替换item.setChecked(true):

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