Set selected item in Android BottomNavigationView

前端 未结 21 2394
谎友^
谎友^ 2020-11-27 14:16

I am using the new android.support.design.widget.BottomNavigationView from the support library. How can I set the current selection from code? I realized, that

21条回答
  •  时光说笑
    2020-11-27 15:05

    private void setSelectedItem(int actionId) {
        Menu menu = viewBottom.getMenu();
        for (int i = 0, size = menu.size(); i < size; i++) {
            MenuItem menuItem = menu.getItem(i);
            ((MenuItemImpl) menuItem).setExclusiveCheckable(false);
            menuItem.setChecked(menuItem.getItemId() == actionId);
            ((MenuItemImpl) menuItem).setExclusiveCheckable(true);
        }
    }
    

    The only 'minus' of the solution is using MenuItemImpl, which is 'internal' to library (though public).

提交回复
热议问题