我已经在带有片段的 Android 中实现了 NavigationView。这是我第一次处理碎片。问题是我在 NavigationView 中有 3 个项目,并且想做这样的事情:
- 用户进入主屏幕,默认选择“前 20 个片段”(此时返回键使应用退出)-我将此片段设置为默认值,而不将事务拉回堆栈,
private void setStartingFragment(NavigationView navigationView,
FragmentManager fragmentManager) {
Fragment fragment = null;
Class fragmentClass = Top20RecipesFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout_content, fragment);
fragmentTransaction.commit();
MenuItem menuItem = navigationView.getMenu().getItem(0);
menuItem.setChecked(true); // Highlight The Selected Item
setTitle(menuItem.getTitle()); // Updating Toolbar Title
}
当我选择其他 2 个片段之一时,我希望它们添加到后堆栈。我想在 Twitch 移动应用程序中这样做。可能性是:
- 第二个片段(或第三个)-> 主页片段-> 退出或
- 2nd Fragment -> 3rd Fragment -> Home Fragment -> 退出或
- 2nd Fragment -> 3rd Fragment -> Home Fragment -> 退出或
另一个大问题是在按下后退按钮时同步 NavigationView 选定项目和 Tooblar 标题。
如果我们谈论 NavigationView 和片段替换,这就是我现在所拥有的:
// Replace Existing Fragment With a New One
public void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass = null;
switch(menuItem.getItemId()) {
case R.id.nav_top20_recipes: {
fragmentClass = Top20RecipesFragment.class;
break;
}
case R.id.nav_kitchen_type: {
fragmentClass = KitchenTypeFragment.class;
break;
}
case R.id.nav_meal_type: {
fragmentClass = MealTypeFragment.class;
break;
}
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout_content, fragment);
fragmentTransaction.addToBackStack(Integer.toString(fragment.getId()));
fragmentTransaction.commit();
menuItem.setChecked(true); // Highlight The Selected Item
setTitle(menuItem.getTitle()); // Updating Toolbar Title
mDrawerLayout.closeDrawers(); // Close The Drawer
}
我查看了 Android 官方网站,他们建议使用 OnBackStackChangedListener 根据片段替换和使用后退按钮来更改 Activity 外观。
我尝试应用此界面,在这里我尝试使用不同的教程进行操作,但应用程序崩溃或无法正常工作。
// Managing Fragments In Back Stack
@Override
public void onBackStackChanged() {
int actualStackHeight = getSupportFragmentManager().getBackStackEntryCount();
if (actualStackHeight > 0) {
FragmentManager fragmentManager = getSupportFragmentManager();
int fragmentId = fragmentManager.getBackStackEntryAt(actualStackHeight - 1).getId();
Fragment currentFragment = fragmentManager.findFragmentById(fragmentId);
switch (currentFragment.getId()) {
case R.id.top20_fragment: {
MenuItem menuItem = nvDrawer.getMenu().getItem(0);
menuItem.setChecked(true); // Highlight The Selected Item
setTitle(menuItem.getTitle()); // Updating Toolbar Title
}
case R.id.kitchen_type_fragment: {
MenuItem menuItem = nvDrawer.getMenu().getItem(1);
menuItem.setChecked(true); // Highlight The Selected Item
setTitle(menuItem.getTitle()); // Updating Toolbar Title
}
case R.id.meal_type_fragment: {
MenuItem menuItem = nvDrawer.getMenu().getItem(2);
menuItem.setChecked(true); // Highlight The Selected Item
setTitle(menuItem.getTitle()); // Updating Toolbar Title
}
}
} else {
finish();
}
}
这是我第一次处理这样的事情,所以请试着理解我。我什至在堆栈上搜索了不同的答案,但没有一个帮助我:/