How to change fragments using Android navigation drawer

后端 未结 5 1083
忘了有多久
忘了有多久 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:01

    The response of DenisGl, is the right way !!! Using the class member, created by default, you can switch between the various components of the Navigation Drawer !! You have to use the method onCreateView, which are within the class member PlaceholderFragment. This class will be automatically invoked in the method onNavigationDrawerItemSelected

    Here is the code example: /This method can be left as it is! It automatically invoke the class PlaceholderFragment!/

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }
    

    This method instead, where you enter the Switch Case:

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = null;
            rootView = inflater.inflate(R.layout.fragment_home, container, false);
            switch(getArguments().getInt(ARG_SECTION_NUMBER)) {
                case 1:
                    rootView = inflater.inflate(R.layout.fragment_home, container, false);
                    break;
                case 2:
                    //rootView = inflater.inflate(R.layout.fragment_obj_list, container, false);
                    break;
                case 3:
                    //rootView = inflater.inflate(R.layout.fragment_obj_list, container, false);
                    break;
                case 4:
                    rootView = inflater.inflate(R.layout.fragment_info, container, false);
                    break;
            }
            return rootView;
        }
    

    Obviously, you have to call the layout for each fragment that interests us!

    This works 100%, Test to verify!

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-30 23:04

    It does work in Eclipse IDE

       switch(getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1:
                rootView = inflater.inflate(R.layout.fragment_home, container, false);
                break;
            case 2:
                //rootView = inflater.inflate(R.layout.fragment_obj_list, container, false);
                break;
            case 3:
                //rootView = inflater.inflate(R.layout.fragment_obj_list, container, false);
                break;
            case 4:
                rootView = inflater.inflate(R.layout.fragment_info, container, false);
                break;
        }
    

    in function

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = null;
        rootView = inflater.inflate(R.layout.fragment_home, container, false);
        return rootView;
    }
    
    0 讨论(0)
  • 2020-11-30 23:09

    I solved this problem over inflater:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView;
            switch(getArguments().getInt(ARG_SECTION_NUMBER)) {
                case 1:
                    rootView = inflater.inflate(R.layout.fragment_obj_detail, container, false);
                    break;
                case 2:
                    rootView = inflater.inflate(R.layout.fragment_obj_list, container, false);
                    break;
                case 3:
                    rootView = inflater.inflate(R.layout.fragment_obj_list, container, false);
                    break;
                case 4:
                    rootView = inflater.inflate(R.layout.fragment_about, container, false);
                    break;
                default:
                    rootView = inflater.inflate(R.layout.fragment_obj_list, container, false);
            }
            return rootView;
        }
    
    0 讨论(0)
  • 2020-11-30 23:10

    You need to create a switch block inside the onNavigationDrawerItemSelected() and use the code that's already there for every case but with corresponding Fragment instead of PlaceholderFragment. Now it contains a generic piece of code for adding a PlaceholderFragment to the layout, reuse it for your purposes.

    0 讨论(0)
提交回复
热议问题