无法更改 NavigationView 中子项的图标
Can't change icons for subitems in NavigationView
我正在尝试在 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;
}
});
menu.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"/>
我发现图标不再变化(即使侦听器中的代码仍在调用)。谁能告诉我为什么这些项目没有更新?
我想知道是否可以在 NavigationView 上调用 invalidateMenuOptions()
或是否可以在支持数据上调用 notifyDataSetChanged()
功能?
首先确保你的两个item不应该是subitem。因为我也面临同样的问题,更改subitem的图标不能正常工作。
所以你的 XML 应该是这样的。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="ng.edu.binghamuni.bhu.ui.activity.HomeActivity">
<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"/>
</menu>
在您的导航上单击执行以下代码。
navView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.books:
menuItem.setIcon((isBookChecked)?R.drawable.btn_check_off_holo_light:R.drawable.btn_check_on_holo_light);
isBookChecked = !isBookChecked;
break;
case R.id.cddvds:
menuItem.setIcon((isCdDVDChecked)?R.drawable.btn_check_off_holo_light:R.drawable.btn_check_on_holo_light);
isCdDVDChecked = !isCdDVDChecked;
break;
}
return true;
}
});
只需在全局创建两个变量 isBookChecked
和 isCdDVDChecked
以手动维护检查状态。
[更新--------------------/////]
我花了一些时间来更改子项的图标。 我终于做到了。但目前这是一个棘手的解决方案。
因为一个月前我报告了一个错误:
https://code.google.com/p/android/issues/detail?id=176300,我们无法将动态菜单添加到导航视图。我认为这也是该错误的链条(更新 UI 问题)。
所以,对于解决方案,请在您的导航上单击以下内容..
navView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// change the icon as you need
menuItem.setIcon(getResources().getDrawable(android.R.drawable.checkbox_on_background));
// here is the trick. Resetting the title of top level item.
navView.getMenu().getItem(0).setTitle(navView.getMenu().getItem(0).getTitle());
return true;
}
});
实际上,当我们使用 setIcon() 方法更改图标时。图标正在更改,但问题是它不会更新 UI。 因此重置顶级项目的标题将更新 UI。希望对你有所帮助。
我正在尝试在 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;
}
});
menu.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"/>
我发现图标不再变化(即使侦听器中的代码仍在调用)。谁能告诉我为什么这些项目没有更新?
我想知道是否可以在 NavigationView 上调用 invalidateMenuOptions()
或是否可以在支持数据上调用 notifyDataSetChanged()
功能?
首先确保你的两个item不应该是subitem。因为我也面临同样的问题,更改subitem的图标不能正常工作。
所以你的 XML 应该是这样的。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="ng.edu.binghamuni.bhu.ui.activity.HomeActivity">
<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"/>
</menu>
在您的导航上单击执行以下代码。
navView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.books:
menuItem.setIcon((isBookChecked)?R.drawable.btn_check_off_holo_light:R.drawable.btn_check_on_holo_light);
isBookChecked = !isBookChecked;
break;
case R.id.cddvds:
menuItem.setIcon((isCdDVDChecked)?R.drawable.btn_check_off_holo_light:R.drawable.btn_check_on_holo_light);
isCdDVDChecked = !isCdDVDChecked;
break;
}
return true;
}
});
只需在全局创建两个变量 isBookChecked
和 isCdDVDChecked
以手动维护检查状态。
[更新--------------------/////]
我花了一些时间来更改子项的图标。 我终于做到了。但目前这是一个棘手的解决方案。 因为一个月前我报告了一个错误: https://code.google.com/p/android/issues/detail?id=176300,我们无法将动态菜单添加到导航视图。我认为这也是该错误的链条(更新 UI 问题)。
所以,对于解决方案,请在您的导航上单击以下内容..
navView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// change the icon as you need
menuItem.setIcon(getResources().getDrawable(android.R.drawable.checkbox_on_background));
// here is the trick. Resetting the title of top level item.
navView.getMenu().getItem(0).setTitle(navView.getMenu().getItem(0).getTitle());
return true;
}
});
实际上,当我们使用 setIcon() 方法更改图标时。图标正在更改,但问题是它不会更新 UI。 因此重置顶级项目的标题将更新 UI。希望对你有所帮助。