旋转菜单时,导航抽屉中动态(以编程方式)创建的子菜单出现问题。我的导航视图菜单xml仅包含主菜单(见下文)。调用onCreate事件时添加子菜单。一切正常,直到我旋转屏幕 - 子菜单中唯一显示的是它的标签。我试图调查这个问题,并尝试使用静态变量,但没有成功。
你能告诉我我的代码有什么问题吗?
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single" android:id="@+id/main_group">
<item
android:id="@+id/leagues_in_progress_item"
android:icon="@drawable/ic_format_line_spacing_black_48dp"
android:title="@string/title_activity_leagues_in_progress" />
<item
android:id="@+id/last_matches_item"
android:icon="@drawable/ic_access_alarm_black_48dp"
android:title="@string/title_activity_last_matches" />
<item
android:id="@+id/archive_item"
android:icon="@drawable/ic_folder_open_black_48dp"
android:title="@string/title_activity_archive" />
<item
android:id="@+id/put_score_item"
android:icon="@drawable/ic_add_circle_outline_black_48dp"
android:title="@string/title_activity_put_score" />
</group>
</menu>
HomeActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.pinokio);
this.mDrawerLayout = (DrawerLayout) findViewById(R.id.pinokioLayout);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
this.mDrawerToggle = new ActionBarDrawerToggle(this, this.mDrawerLayout, R.string.app_name, R.string.app_name);
this.mDrawerLayout.setDrawerListener(this.mDrawerToggle);
HomeActivity.mNavigationView = (NavigationView) findViewById(R.id.left_drawer);
this.addLeaguesSubmenu();
HomeActivity.mNavigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
displayMenuFragment(menuItem);
return true;
}
});
if (savedInstanceState == null) {
this.displayMenuFragment(HomeActivity.mNavigationView.getMenu().getItem(0));
}
}
/**
* Add submenu that contains leagues in progress
* @return HomeActivity
*/
private void addLeaguesSubmenu()
{
if (this.internetConnection.isOnline()) {
if (HomeActivity.menuLeagues == null) {
Intent intent = this.getIntent();
HomeActivity.menuLeagues = intent.getParcelableArrayListExtra("leagues");
}
this.leaguesSubmenu.generate(HomeActivity.mNavigationView, HomeActivity.menuLeagues);
}
}
这就是生成子菜单的方法:
@Override
public void generate(NavigationView navigationView, ArrayList<League> leagues)
{
Menu menu = navigationView.getMenu();
SubMenu leaguesSubMenu = menu.addSubMenu(this.context.getResources().getString(R.string.title_activity_leagues_in_progress));
League league;
MenuItem menuItem;
for (int i = 0; i<leagues.size(); i++) {
league = leagues.get(i);
menuItem = leaguesSubMenu.add(league.getShortName());
Intent intent = new Intent();
intent.putExtra("leagueId", league.getId());
menuItem.setIntent(intent);
if (!league.getFoosballMode()) {
menuItem.setIcon(R.drawable.ic_fifa);
}
else {
menuItem.setIcon(R.drawable.ic_foosball);
}
}
}
答案 0 :(得分:1)
我相信这可能是因为旋转时View会重新充气,但你的代码是onCreate,所以如果是这样的话,你有两个选择。您可以在视图重新定义后重新生成菜单,或将其添加到AndroidManifest.xml
文件中的活动中:
android:configChanges="orientation|screenSize"
此外,如果您有兴趣在此处参考有关处理配置更改的信息,例如轮换:http://developer.android.com/guide/topics/resources/runtime-changes.html