我已经实现了新的BottomNavigationView(com.android.support:design:25.0.0),并不知道如何设置选择索引或MenuItem
id(在我的情况中,应该被默认选中)。恐怕现在没有这种可能性,只要它太原始了,但无论如何,任何帮助将不胜感激。谢谢!在BottomNavigationView中设置最初选择的项索引/ ID
回答
设置使用setSelectedItemId
选择的菜单项ID:
bottomNavigationView.setSelectedItemId(R.id.item_id);
这种方法开始,购自Android的支持库25.3.0。
为我工作,唯一的解决办法是:
View view = bottomNavigationView.findViewById(R.id.menu_action_dashboard);
view.performClick();
只需执行点击的伎俩。希望我们在未来的版本中会有更多的方法/属性。
UPD:
由于user5968678mentioned,由于Android支持库v25.3.0加入新的方法:
bottomNavigationView.setSelectedItemId(R.id.item_id);
所以使用,而不是:)
最佳解决方案 – Meanman
您可以扩展BottomNavigationView和使用反射来调用私有方法。
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);
}
}
反射是一个坏主意! –
停止使用反射!这是坏的!
好,而支持库不给我们上显示时,它是可见的第一次选择从BottomNavigationView项目的选择,我们有两种可能性:
首先,用循环:
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;
}
});
}
其次,不循环,但与类变量(因为所述逻辑被从内部类内完成):
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()方法被执行?在活动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.getMenu().getItem(menuItemIndex).setChecked(true)
其中menuItemIndex是所选元素的索引。
不起作用。如果您在bottomNavigationBar上启用了动画,则可以看到所选项目上的颜色已更改,但动画仍在第一个菜单项上。现在在android lib中不可用。如果你想让它工作:((BottomNavigationMenuView)bottomNavigationView.getChildAt(0)).getChildAt(menuItemIndex).performClick();或者甚至更好:((BottomNavigationMenuView)bottomNavigationView.getChildAt(0))。findViewById(R.id.id_menu_action).performClick(); –
你可以发布你的代码吗?我使用谷歌支持库(25.1.0)的最新BottomNavigationView,当我改变选定的索引时,动画没有问题。 –
这里是the documentation说一下:
菜单项也可用于编程方式选择哪个目的地是当前活动。它可以通过
MenuItem#setChecked(true)
作为替代到什么一月发布来完成,也可以通过ID找到该项目:
Menu menu = findViewById(R.id.navigation).getMenu();
menu.findItem(R.id.navigation_home).setChecked(true);
而且,我可以推荐打电话.callOnClick()
instead of .performClick()
。
如果您使用监听器,像在Android Studio中默认的实现,试试这个:
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
Integer indexItem = 4;
navigation.getMenu().getItem(indexItem).setChecked(true);
mOnNavigationItemSelectedListener.onNavigationItemSelected(navigation.getMenu().getItem(indexItem));
- 1. BottomNavigationView设置选项与ViewPager选择Xamarin
- 2. UITableViewCell设置选择最初
- 3. DataGridViewComboBoxColumn设置选择索引
- 4. 如何设置最初选择的选项卡ActionBarSherlock
- 5. 设置选项卡索引
- 6. 如何在BottomNavigationView中设置选定的项目
- 7. 如何在Kotlin中设置默认的BottomNavigationView选项卡?
- 8. 的Android动作条选项卡上设置最初选择的选项卡
- 9. GXT:如何设置最初选中的复选框TreeGrid项目?
- 10. 如何使用jsp在html中设置select的选择索引
- 11. 在Telerik MVC Grid中的Dropdownlist上设置选择的索引
- 12. ListFragment设置初始选择
- 13. 剃刀Html选择/选项设置选择选项在回发
- 14. 在屏幕旋转过程中保存BottomNavigationView选择的项目
- 15. 项目在android中选择的颜色BottomNavigationView
- 16. 在选项中设置多个ID
- 17. 设置设置选择的索引后Listpicker变空
- 18. 如何在javascript中为引导选项卡设置ID
- 19. Angular ng选项选择未设置为初始值
- 20. 在选择列表中设置项目的最大数量 - html
- 21. 选择模糊选项最快的搜索引擎
- 22. 在角度选择中设置选择选项
- 23. 设置选择选项
- 24. Rails form_for设置选项和设置选择的选项
- 25. 选择排序 - 最小/最大索引
- 26. 设置在日期选择器中选择的最短日期
- 27. 将JComboBox的初始选择索引设置为-1或者什么都不是
- 28. JSF F:selectItems的设置项目中选择
- 29. 如何将TableViewer中的选择设置为指定的索引?
- 30. 如何在jQuery中设置选择选项的选定值?
请参阅此链接。 http://androidgifts.com/build-android-material-design-bottom-navigation/ –
是的,我知道[BottomBar](https://github.com/roughike/BottomBar),但我无法相信support.design的一个没有可能以编程方式选择项目:)也许一些hacky解决方案... – UneXp
看看[这个答案](http://stackoverflow.com/a/40244165/2215962)。这是最好的解决方案。 –