gpt4 book ai didi

android - NavigationDrawer - 以编程方式创建的子菜单在屏幕旋转后消失

转载 作者:行者123 更新时间:2023-11-30 01:59:15 28 4
gpt4 key购买 nike

我在旋转菜单时在抽屉导航中动态(以编程方式)创建子菜单时遇到问题。我的导航 View 菜单 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);
}

}
}

最佳答案

我相信这可能是因为旋转时 View 会重新膨胀,但您的生成代码位于 onCreate 中,因此如果是这种情况,您有两个选择。您可以在 View 重新膨胀时重新生成菜单,或者将其添加到 AndroidManifest.xml 文件中的 Activity 中:

android:configChanges="orientation|screenSize"

此外,如果您感兴趣,请参阅此处以获取有关处理配置更改(例如旋转)的信息:http://developer.android.com/guide/topics/resources/runtime-changes.html

关于android - NavigationDrawer - 以编程方式创建的子菜单在屏幕旋转后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746358/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com