且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android使用导航抽屉加载片段

更新时间:2022-12-29 07:56:33

根据Java文档

默认部分处理所有未明确处理的值 通过其中一个案例部分.

The default section handles all values that are not explicitly handled by one of the case sections.

您可以在此处查找更多,而且我会建议您这样做:

You can look here for more and also I would suggest you do to it smth like this:

@Override
        public boolean onNavigationItemSelected(MenuItem item) {
            int id = item.getItemId();
            Fragment fragment = null;
            if (id == R.id.home) {
           fragment = new HomeFragment();
            } else if (id == R.id.nav_contact) {
           fragment = new ContactFragment();
            } else if (id == R.id.nav_article) {
               fragment = new ArticleFragment();
            } else if (id == R.id.nav_notifications) {
             fragment= new NotificationsFragment();
            }  else if (id == R.id.research) {
             fragment= new RechercheFragment();
            }

            if (fragment != null) {

                myFragmentTransaction = fragmentManager.beginTransaction();
                myFragmentTransaction.replace(R.id.frame, fragment);
                myFragmentTransaction.addToBackStack(null);
                myFragmentTransaction.commit();

            }

                item.setChecked(true);
            setTitle(item.getTitle());

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);

            return super.onOptionsItemSelected(item);

        }
    }