自动更改 select 为 bottonavigation selected

change automatically select for bottonavigation selected

我正在处理这段代码

private void setupViews(){
    frameLayout = (FrameLayout) findViewById(R.id.frame_id);
    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
    getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    int navID = menuItem.getItemId();
    switch (navID){
        case R.id.home:
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new HomeFragment()).commit();
            break;
        case R.id.search:
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();
            break;
        case R.id.profile:
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new ProfileFragment()).commit();
            break;
    }
    return true;
}

当我启动我的应用程序时,它会自动进入第一个底部(我有 3 个底部) 我想将其更改为导航视图中的第二个底部。请帮助我

@iamhanniballake

我在主程序中调用 setupViews() activity 我有3个底 -1-2-3- 当我的应用 运行 打开带有底部导航视图的片段时 它自动选择了 3 我想将其更改为 2

试试这个:

bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_id);
bottomNavigationView.setSelectedItemId(R.id.search);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_id,new SearchFragment()).commit();