使用NavigationUI 加载Fragment,如何获取Fragment对象
作者:互联网
1. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); // NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); navView.setItemIconTintList(null); // 设置 BottomNavigationView 图片没有颜色覆盖 NavigationUI.setupWithNavController(navView, navController);
2. 获取Fragment的代码
yangaiyu 发布了30 篇原创文章 · 获赞 44 · 访问量 11万+ 私信 关注public Fragment getFragment(Class<?> clazz) { List<Fragment> fragments = getSupportFragmentManager().getFragments(); if (fragments!= null && fragments.size() > 0) { NavHostFragment navHostFragment = (NavHostFragment) fragments.get(0); List<Fragment> childfragments = navHostFragment.getChildFragmentManager().getFragments(); if(childfragments != null && childfragments.size() > 0){ for (int j = 0; j < childfragments.size(); j++) { Fragment fragment = childfragments.get(j); if(fragment.getClass().isAssignableFrom(clazz)){ Log.i(TAG, "getFragment1: " + fragment); return fragment; } } } } return null; }
标签:NavigationUI,null,Fragment,fragment,fragments,childfragments,加载 来源: https://blog.csdn.net/yangaiyu/article/details/104424560