How to change fragments using Android navigation drawer

后端 未结 5 1103
忘了有多久
忘了有多久 2020-11-30 22:43

I know these types of question have already been here but still I have not found my answer for this question:

  • I have created an application and used navigatio
5条回答
  •  爱一瞬间的悲伤
    2020-11-30 23:02

    You should just put a switch statement into the onNavigationDrawerItemSelected method.

    Something like this should work:

    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        Fragment fragment;
        FragmentManager fragmentManager = getFragmentManager(); // For AppCompat use getSupportFragmentManager
        switch(position) {
            default:
            case 0:
                fragment = new MyFragment1();
                break;
            case 1:
                fragment = new MyFragment2();
                break;
        }
        fragmentManager.beginTransaction()
            .replace(R.id.container, fragment)
            .commit();
    }
    

    This is just done quickly but I think it should work

提交回复
热议问题