在BottomNavigationView中设置最初select的项目索引/ ID

我已经实现了新的BottomNavigationView (com.android.support:design:25.0.0),不知道如何设置select索引或MenuItem id(在我的情况下,中间项目应该被选中默认情况下)。 恐怕现在没有这样的可能性,只要它太原始了,但无论如何,任何帮助将不胜感激。 谢谢!

Solutions Collecting From Web of "在BottomNavigationView中设置最初select的项目索引/ ID"

使用setSelectedItemId设置选定的菜单项ID:

 bottomNavigationView.setSelectedItemId(R.id.item_id); 

此方法从Android支持库25.3.0开始提供。

我唯一的解决scheme是:

 View view = bottomNavigationView.findViewById(R.id.menu_action_dashboard); view.performClick(); 

只需执行点击即可。 希望在将来的版本中会有更多的方法/属性。

UPD:

正如user5968678 提到的那样 ,自从Android Support Library v25.3.0增加了新的方法:

 bottomNavigationView.setSelectedItemId(R.id.item_id); 

所以用来代替:)

我认为这个解决scheme比接受的答案稍微优雅一些​​:

 bottomNavigationView.getMenu().getItem(menuItemIndex).setChecked(true) 

其中menuItemIndex是所选元素的索引。

以下是文档所述的内容:

菜单项也可以用于以编程方式select哪个目的地当前处于活动状态。 它可以使用MenuItem#setChecked(true)

作为Jan发布的替代scheme,您还可以通过idfind该项目:

 Menu menu = findViewById(R.id.navigation).getMenu(); menu.findItem(R.id.navigation_home).setChecked(true); 

另外,我可以推荐调用.callOnClick()而不是.performClick()

停止使用reflection! 这是坏的!

那么,虽然支持库没有给我们提供从底部导航视图中select项目以便在第一次显示时显示的选项,但是我们有两种可能性:

首先,使用循环:

 private void setupBottomNavigationView() { // Get the menu from our navigationBottomView. Menu bottomNavigationViewMenu = bottomNavigationView.getMenu(); // Uncheck the first menu item (the default item which is always checked by the support library is at position 0). bottomNavigationMenu.findItem(R.id.action_one).setChecked(false); // Check the wished first menu item to be shown to the user. bottomNavigationMenu.findItem(R.id.action_two).setChecked(true); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Menu bottomNavigationMenu = bottomNavigationView.getMenu(); for (int i = 0; i < bottomNavigationMenu.size(); i++) { if (item.getItemId() != bottomNavigationMenu.getItem(i).getItemId()) { bottomNavigationMenu.getItem(i).setChecked(false); } } switch (item.getItemId()) { case R.id.action_one : replaceFragment(new OneFragment()); break; case R.id.action_two : replaceFragment(new TwoFragment()); break; case R.id.action_three : replaceFragment(new ThreeFragment()); break; } return false; } }); } 

其次,没有循环,但有一个类variables(因为逻辑是在内部类中完成的):

 private void setupBottomNavigationView() { // Get the menu from our navigationBottomView. Menu bottomNavigationViewMenu = bottomNavigationView.getMenu(); // Uncheck the first menu item (the default item which is always checked by the support library is at position 0). bottomNavigationViewMenu.findItem(R.id.action_one).setChecked(false); // Check the wished first menu item to be shown to the user. Also store that menu item on a variable to control when a menu item must be unchecked. mActiveBottomNavigationViewMenuItem = bottomNavigationViewMenu.findItem(R.id.action_two).setChecked(true); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem selectedMenuItem) { switch (selectedMenuItem.getItemId()) { case R.id.action_one : replaceFragment(new OneFragment()); break; case R.id.action_two : replaceFragment(new TwoFragment()); break; case R.id.action_three : replaceFragment(new ThreeFragment()); break; } if (selectedMenuItem != mActiveBottomNavigationViewMenuItem){ mActiveBottomNavigationViewMenuItem.setChecked(false); mActiveBottomNavigationViewMenuItem = selectedMenuItem; } return false; } }); } private MenuItem mActiveBottomNavigationViewMenuItem; 

在执行setupBottomNavigationView()方法时? 在Activity的onCreate()方法中,看看:

 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ... setupBottomNavigationView(); } 

简单,没有广泛的代码。

希望能帮助到你!

试试这个代码

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); bottomNavigationView.setOnNavigationItemSelectedListener(this); Menu menu = bottomNavigationView.getMenu(); this.onNavigationItemSelected(menu.findItem(R.id.action_favorites)); } 

您可以扩展BottomNavigationView并使用reflection来调用私有方法。

 public class SelectableBottomNavigationView extends BottomNavigationView { public SelectableBottomNavigationView(Context context) { super(context); } public SelectableBottomNavigationView(Context context, AttributeSet attrs) { super(context, attrs); } public SelectableBottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void setSelected(int index) { try { BottomNavigationMenuView menuView = (BottomNavigationMenuView) getField(BottomNavigationView.class, "mMenuView"); OnNavigationItemSelectedListener listener = (OnNavigationItemSelectedListener) getField(BottomNavigationView.class, "mListener"); try { Method method = menuView.getClass().getDeclaredMethod("activateNewButton", Integer.TYPE); method.setAccessible(true); // activate item. method.invoke(menuView, index); if (listener != null) { // trigger item select event. listener.onNavigationItemSelected(getMenu().getItem(index)); } } catch (SecurityException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } private Object getField(Class clazz, String fieldName) throws NoSuchFieldException, IllegalAccessException { Field f = clazz.getDeclaredField(fieldName); f.setAccessible(true); return f.get(this); } }