1

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

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

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

我将显示toast 消息的逻辑else部分,既不希望 BottomNavigationView 移动,也不希望更改菜单图标颜色。

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

4

1 回答 1

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);
}
于 2017-10-10T04:59:51.447 回答