BottomNavigationView启用单击禁用检查

时间:2018-08-27 07:01:53

标签: android android-layout android-view android-menu bottomnavigationview

我正在使用一个BottomNavigationView,其中包含5个项目,4个片段和1个活动,如下图所示

Image

单击任何片段时,我希望它正常运行,得到clickedchecked,但是单击“ +”项时,我正在打开一个活动,但我不希望它是checked,所以我希望它是clickable,以便它可以打开活动,但我不想是checked,因为当用户从活动中返回时,会看到即使选择了错误的项目,它也会被选中。 我该怎么办?

这是我的代码:

bottomNavigationView = findViewById(R.id.bottom_nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(this);

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    int id = menuItem.getItemId();
    Fragment fragment = null;
    switch (id) {
        case R.id.home_nav_menu:
            fragment = new HomeFragment();
            break;
        case R.id.inbox_nav_menu:
            fragment = new InboxFragment();
            break;
        case R.id.add_nav_menu:
            Intent intent = new Intent(this, AddActivity.class);
            startActivity(intent);
            return true;
        case R.id.history_nav_menu:
            fragment = new HistoryFragment();
            break;
        case R.id.profile_nav_menu:
            fragment = new ProfileFragment();
            break;
    }
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_place_holder, fragment).commit();
    return true;
}

3 个答案:

答案 0 :(得分:2)

请参阅侦听器的源代码以进行底部导航,当您不想显示所选内容时,只需返回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);
    }

因此,您只需在case R.id.add_nav_menu:中返回false即可。

答案 1 :(得分:0)

用户MenuItem#setCheckable(boolean) API才能播放菜单项的可检查状态。


    MenuItem menuItem = navigation.getMenu().getItem(1);
    menuItem.setCheckable(false);

enter image description here

答案 2 :(得分:0)

尝试这样:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Fragment fragment = null;
switch (id) {
    case R.id.home_nav_menu:
        fragment = new HomeFragment();
        break;
    case R.id.inbox_nav_menu:
        fragment = new InboxFragment();
        break;
    case R.id.add_nav_menu:
  menuItem.setCheckable(false);
        Intent intent = new Intent(this, AddActivity.class);
        startActivity(intent);
        return true;
    case R.id.history_nav_menu:
        fragment = new HistoryFragment();
        break;
    case R.id.profile_nav_menu:
        fragment = new ProfileFragment();
        break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_place_holder, fragment).commit();
return true;
}
相关问题