我可以在特定逻辑上禁用特定BottomNavigationView项目的移位吗?

时间:2017-10-10 04:45:36

标签: android android-layout bottomnavigationview android-bottomnav

目前我有一个像这样的实现 -

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

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

逻辑 else 部分,我将在其中显示 Toast消息,我既不希望BottomNavigationView转换也不想更改菜单图标颜色。

我如何才能在这个特定部分实现这样的逻辑?所有其他菜单项将保持默认的移位逻辑。

1 个答案:

答案 0 :(得分:1)

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