我可以根据特定逻辑禁用特定 BottomNavigationView 项目的移动吗?
Can I disable shifting in specific BottomNavigationView item on specific logic?
目前我有这样的实现-
mBottomNav.setOnNavigationItemSelectedListener(
......
switch (item.getItemId()) {
case R.id.actionUser:
if(userLoggedIn) {
mHomePresenter.btnUser();
}
else {
showLongToast("User Not Logged In");
}
break;
});
逻辑 else 部分,我将在其中显示 toast 消息,既不希望 BottomNavigationView 移动也不希望更改菜单图标颜色。
如何只在这个特定的部分实现这样的逻辑?所有其他菜单项将保留默认的移动逻辑。
嗯,答案很简单,对于您希望发生过渡的条件,return true
对于您不希望发生的条件 return false
考虑到您的代码,它应该是
mBottomNav.setOnNavigationItemSelectedListener(
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.actionUser:
if(userLoggedIn) {
mHomePresenter.btnUser();
return true;
}
else {
showLongToast("User Not Logged In");
return false;
}
}
});
如果您查看导航选择侦听器的文档,您可以看到
/**
* Listener for handling selection events on bottom navigation items.
*/
public interface OnNavigationItemSelectedListener {
/**
* Called when an item in the bottom navigation menu is selected.
*
* @param item The selected item
*
* @return true to display the item as the selected item and false if the item should not
* be selected. Consider setting non-selectable items as disabled preemptively to
* make them appear non-interactive.
*/
boolean onNavigationItemSelected(@NonNull MenuItem item);
}
目前我有这样的实现-
mBottomNav.setOnNavigationItemSelectedListener(
......
switch (item.getItemId()) {
case R.id.actionUser:
if(userLoggedIn) {
mHomePresenter.btnUser();
}
else {
showLongToast("User Not Logged In");
}
break;
});
逻辑 else 部分,我将在其中显示 toast 消息,既不希望 BottomNavigationView 移动也不希望更改菜单图标颜色。
如何只在这个特定的部分实现这样的逻辑?所有其他菜单项将保留默认的移动逻辑。
嗯,答案很简单,对于您希望发生过渡的条件,return true
对于您不希望发生的条件 return false
考虑到您的代码,它应该是
mBottomNav.setOnNavigationItemSelectedListener(
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.actionUser:
if(userLoggedIn) {
mHomePresenter.btnUser();
return true;
}
else {
showLongToast("User Not Logged In");
return false;
}
}
});
如果您查看导航选择侦听器的文档,您可以看到
/**
* Listener for handling selection events on bottom navigation items.
*/
public interface OnNavigationItemSelectedListener {
/**
* Called when an item in the bottom navigation menu is selected.
*
* @param item The selected item
*
* @return true to display the item as the selected item and false if the item should not
* be selected. Consider setting non-selectable items as disabled preemptively to
* make them appear non-interactive.
*/
boolean onNavigationItemSelected(@NonNull MenuItem item);
}