我在Android中使用片段实现了NavigationView。这是我第一次处理片段。问题是我在NavigationView中有3个项目,并希望做类似的事情:
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移动应用程序中这样做。可能性是:
另一个大问题是按下后退按钮同步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外观。
我试图应用这个界面,在这里我尝试用不同的toutorials但应用程序崩溃或无法正常工作。
// 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();
}
}
这是我第一次处理这样的事情,所以请尽量了解我。我甚至在堆栈上搜索了不同的ansewers,但没有他们帮助我:/
答案 0 :(得分:0)
如果您使用NavigationView
,请结合使用它和Navigation API。
然后,使用您的onCreate(..)
方法...
main_navigationview.setupWithNavController(
findNavController(R.id.main_nav_container)
)
findNavController(
R.id.main_nav_container
).addOnDestinationChangedListener {
controller, destination, bundle ->
main_navigationview.setCheckedItem(destination.id)
}
然后,NavigationView
将同步,就这么简单。