问题
I have a BottomNavigationView in my activity with onNavigationItemSelected listener.
I have two questions:
Does
setSelectedItemIdmethod invoke theonNavigationItemSelectedmethod? The documentation says this method behaves as if the menu item was tapped, my observation is yes.If yes, how to highlight a menu item without invoking
onNavigationItemSelected?
回答1:
Yes,
BottomNavigationView.setSelectedItemId(int itemId)triggersonNavigationItemSelected(MenuItem item).Use
BottomNavigationView.getMenu().getItem(int index)orBottomNavigationView.getMenu().findItem(int id), then callMenuItem.setChecked(boolean checked)to highlight a menu item without triggeringonNavigationItemSelected(MenuItem item).
来源:https://stackoverflow.com/questions/45062620/highlighting-a-menu-item-in-bottomnavigationview-without-invoking-onnavigationit