我正在尝试在NavigationView
没有子项(所有顶级菜单项),我可以使用以下代码愉快地更改图标:
mNavigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Boolean boolObj = mMenuToggleMap.get(menuItem.getItemId());
boolean state = boolObj == null ? true : boolObj.booleanValue();
state = !state;
if(state) menuItem.setIcon(R.drawable.btn_check_off_holo_light);
else menuItem.setIcon(R.drawable.btn_check_on_holo_light);
mMenuToggleMap.put(menuItem.getItemId(), state);
return true;
}
});
菜单.xml:
<item
android:id="@+id/books"
android:title="@string/books"
android:icon="@drawable/btn_check_off_holo_light"
app:showAsAction="always"/>
<item
android:id="@+id/cddvds"
android:icon="@drawable/btn_check_off_holo_light"
android:title="@string/cddvds"
app:showAsAction="always"/>
如你看到的:
但是,一旦我像这样更改我的 XML:
<item
android:id="@+id/categorySubHeader"
android:title="@string/categories">
<menu>
<item
android:id="@+id/books"
android:title="@string/books"
android:icon="@drawable/btn_check_off_holo_light"
app:showAsAction="always"/>
<item
android:id="@+id/cddvds"
android:icon="@drawable/btn_check_off_holo_light"
android:title="@string/cddvds"
app:showAsAction="always"/>
我发现图标不再更改(即使仍在调用侦听器中的代码)。谁能告诉我为什么这些项目没有更新?
我想知道是否invalidateMenuOptions()
可以在 NavigationView 上调用或者notifyDataSetChanged()
可以在支持数据上调用功能?