在我的bottomNavigationView中,我可以更改programmaticaly我的菜单项似乎工作的一切,但实际上我的布局与if检查或直接单击选项卡不同。请看下面的差异:
1)menuItem.setChecked(true)
:
2)当用户直接点击图标时,它将执行动画并在正确的
上显示我真正想要的是选择menuItem,就像用户点击一样。不应该通过执行menuItem.setChecked(true)来工作吗?
请考虑菜单项的以下定义布局:
<item android:id="@+id/menu_home"
android:title="@string/menu_home"
android:icon="@drawable/ic_home_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="@+id/menu_hall_of_fame"
android:title="@string/menu_hall_of_fame"
android:icon="@drawable/ic_stars_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="@+id/menu_info"
android:title="@string/menu_info"
android:icon="@drawable/ic_info_black_24dp"
app:showAsAction="ifRoom" />
<item android:id="@+id/menu_settings"
android:title="@string/menu_settings"
android:icon="@drawable/ic_settings_black_24dp"
app:showAsAction="ifRoom" />
以及我如何检查正确的menuItem:
MenuItem item = mBottomNav.getMenu().findItem(R.id.menu_about);
// update selected item
mSelectedItem = item.getItemId();
// uncheck the other items and select the one.
for (int i = 0; i< mBottomNav.getMenu().size(); i++) {
MenuItem menuItem = mBottomNav.getMenu().getItem(i);
menuItem.setChecked(menuItem.getItemId() == mSelectedItem);
}
还有另一种方法来执行menuItem的选择吗?我在这里缺少什么?
答案 0 :(得分:1)
请参阅此处我发布的所有代码
public class MainActivity extends AppCompatActivity {
private static final String SELECTED_ITEM = "arg_selected_item";
private BottomNavigationView mBottomNav;
private int mSelectedItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectFragment(item);
return true;
}
});
MenuItem selectedItem;
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
} else {
selectedItem = mBottomNav.getMenu().getItem(0);
}
selectFragment(selectedItem);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SELECTED_ITEM, mSelectedItem);
super.onSaveInstanceState(outState);
}
@Override
public void onBackPressed() {
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
if (mSelectedItem != homeItem.getItemId()) {
// select home item
selectFragment(homeItem);
} else {
super.onBackPressed();
}
}
private void selectFragment(MenuItem item) {
Fragment frag = null;
// init corresponding fragment
switch (item.getItemId()) {
case R.id.menu_home:
frag = MenuFragment.newInstance(getString(R.string.text_home),
getColorFromRes(R.color.color_home));
break;
case R.id.menu_notifications:
frag = MenuFragment.newInstance(getString(R.string.text_notifications),
getColorFromRes(R.color.color_notifications));
break;
case R.id.menu_search:
frag = MenuFragment.newInstance(getString(R.string.text_search),
getColorFromRes(R.color.color_search));
break;
}
// update selected item
mSelectedItem = item.getItemId();
// uncheck the other items.
Menu menu = mBottomNav.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem menuItem = menu.getItem(i);
((MenuItemImpl) menuItem).setExclusiveCheckable(false);
menuItem.setChecked(menuItem.getItemId() == R.id.menu_home);
((MenuItemImpl) menuItem).setExclusiveCheckable(true);
}
updateToolbarText(item.getTitle());
if (frag != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, frag, frag.getTag());
ft.commit();
}
}
private void updateToolbarText(CharSequence text) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(text);
}
}
private int getColorFromRes(@ColorRes int resId) {
return ContextCompat.getColor(this, resId);
}
}
Menu menu = mBottomNav.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem menuItem = menu.getItem(i);
((MenuItemImpl) menuItem).setExclusiveCheckable(false);
menuItem.setChecked(menuItem.getItemId() == R.id.menu_home);
((MenuItemImpl) menuItem).setExclusiveCheckable(true);
}