gpt4 book ai didi

android - 如何在 onNavigationItemSelected(MenuItem item) 下调用 Android Studio 1.4 NavigationDrawer 模板中的 fragment

转载 作者:太空狗 更新时间:2023-10-29 14:05:13 28 4
gpt4 key购买 nike

刚刚将 Android Studio 更新到 1.4,我在那里发现了一个与之前不同的 NavigationDrawer 模板。我想通过每个抽屉项目选择调用 fragment ,如 gmail 应用程序。在 public boolean onNavigationItemSelected(MenuItem item) 下,我如何调用不同的 fragment ?

最佳答案

在您的 onNavigationItemSelected(MenuItem item) 中调用 select Drawer Item 方法。

例如,

private void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass;
switch (menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = FamilyGuyFragment.class;
break;
case R.id.nav_second_fragment:
fragmentClass = FuturamaFragment.class;
break;
case R.id.nav_third_fragment:
fragmentClass = SimpsonsFragment.class;
break;
default:
fragmentClass = SouthParkFragment.class;
}


try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}


FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();


menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}

https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer https://github.com/JGeovani/android-navigation-drawer-exercise

关于android - 如何在 onNavigationItemSelected(MenuItem item) 下调用 Android Studio 1.4 NavigationDrawer 模板中的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32923951/

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