gpt4 book ai didi

android - 我可以根据特定逻辑禁用特定 BottomNavigationView 项目的移动吗?

转载 作者:行者123 更新时间:2023-11-30 00:19:03 24 4
gpt4 key购买 nike

目前我有这样的实现-

    mBottomNav.setOnNavigationItemSelectedListener(
......
switch (item.getItemId()) {

case R.id.actionUser:
if(userLoggedIn) {
mHomePresenter.btnUser();
}
else {
showLongToast("User Not Logged In");
}
break;
});

我将在其中显示toast 消息 的逻辑else 部分,我既不想移动BottomNavigationView 也不想改变菜单图标颜色。

我怎样才能只在这个特定的部分实现这样的逻辑?所有其他菜单项将保留默认的移动逻辑。

最佳答案

答案很简单,对于您希望转换发生的条件,返回 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);
}

关于android - 我可以根据特定逻辑禁用特定 BottomNavigationView 项目的移动吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46658752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com