我将“底部导航栏”添加到我的应用程序中,但它可以正常工作,但是如果有条件,我尝试隐藏一个项目或一个按钮,但我尝试不做。
如果需要if(block.equals(“ 3”)),我将看到所有底部导航栏..但如果它等于其他任何数字,则将隐藏一个按钮,而我将看到其他按钮
此按钮我需要隐藏(大小写R.id.nav_s: selectedFragment = new MainActivity();)
我尝试那样操作但不起作用(案例R.id.nav_s: 新的MainActivity()。setMenuVisibility(false);)
这是我的代码:
public class NafMaintest extends AppCompatActivity {
String block;
SessionManager sessionManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_naf_main);
sessionManager = new SessionManager(this);
sessionManager.checkLogin();
HashMap<String, String> user = sessionManager.getUserDetail();
block = user.get(sessionManager.blook);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
if(block.equals("3")){
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_favorites:
selectedFragment = new FragmentFavorites();
break;
case R.id.nav_search:
selectedFragment = new FragmentSearch();
break;
case R.id.nav_s:
selectedFragment = new MainActivity();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
// return false;
}
});
}else {
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_favorites:
selectedFragment = new FragmentFavorites();
break;
case R.id.nav_search:
selectedFragment = new FragmentSearch();
break;
case R.id.nav_s:
new MainActivity().setMenuVisibility(false);//this is I need make it hide if block not equals("3")
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
});
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FragmentFavorites()).commit();
}
}
}
任何人都知道解决方案可以帮助我
答案 0 :(得分:0)
要将项目添加到NavigationView
菜单,请使用bottomNavigationView.getMenu().add()
,要删除项目,请使用bottomNavigationView.getMenu().removeItem()
要将其应用到您的代码中:
public class NafMaintest extends AppCompatActivity {
String block;
SessionManager sessionManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_naf_main);
sessionManager = new SessionManager(this);
sessionManager.checkLogin();
HashMap<String, String> user = sessionManager.getUserDetail();
block = user.get(sessionManager.blook);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_favorites:
selectedFragment = new FragmentFavorites();
break;
case R.id.nav_search:
selectedFragment = new FragmentSearch();
break;
case R.id.nav_s:
selectedFragment = new MainActivity();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
// return false;
}
});
if(block.equals("3")){
if (bottomNavigationView.getMenu().findItem(R.id.nav_s) == null)
bottomNavigationView.getMenu().add(0, R.id.nav_s, 2, "Title"); // 2 is item order in the menu
}else {
if (bottomNavigationView.getMenu().findItem(R.id.nav_s) != null)
bottomNavigationView.getMenu().removeItem(R.id.nav_s);
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FragmentFavorites()).commit();
}
}
}