Switch Fragment with default Android Studio navigation drawer

13,851

Solution 1

(In case you didn't do this) : In AndroidStudio, it is better to generate sample Activity with Navigation Drawer (Alt+Insert -> Activity -> Navigation Drawer Activity (on Android Studio 0.8.+)) You will get activity that hosts NavigationDrawerFragment with some drawables.

In NavigationDrawerFragment onCreateView you populate list items you want to be displayed in the drawer, and activity will automatically implement interface NavigationDrawerFragment.NavigationDrawerCallbacks with method onNavigationDrawerItemSelected(int position). This method is called when you click on item at certain position in Navigation Drawer, and in this method you replace R.id.container with the fragment you need on that position.

You don't have to use PlaceholderFragment, it is the mock to be displayed at first time, before you implement your own. Feel free to delete it and create any fragment you want.

And OnItemClickListener is already implemented in method onCreateView of NavigationDrawerFragment.

Solution 2

The placeholder fragment is just that. It is a blank fragment that is used in the auto-generated code to show you how to use the navigation drawer. You can use any fragment with the navigation drawer.

You can display a fragment when an item is selected in the onNavigationDrawerItemSeleced(int position) that is in the activity that implements NavigationDrawerFragment. You can use a FragmentManager to swap out the old fragment with the new.

Share:
13,851
ArnaudToutain
Author by

ArnaudToutain

Updated on June 05, 2022

Comments

  • ArnaudToutain
    ArnaudToutain almost 2 years

    I'm a little bit lost with the implementation of the navigation drawer in Android Studio. The onCreate method call the PlaceholderFragment class which I don't really understand what is it for.

    But anyway, where should I implement my onItemClickListener to display different fragments according to the item selected in the navigation drawer ?

    This is my current PlaceholderFragment :

    public static class PlaceholderFragment extends Fragment {
    
        private ListView listView;
        private CustomAdapter expenseAdapter;
    
        public PlaceholderFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    
            expenseAdapter = new CustomAdapter(getActivity());
    
            listView = (ListView) rootView.findViewById(R.id.lvExpense);
            listView.setAdapter(expenseAdapter);
            expenseAdapter.loadObjects();
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    String expense = expenseAdapter.getItem(position).get("title").toString();
                    Toast.makeText(getActivity(), expense, Toast.LENGTH_SHORT).show();
                }
            });
    
            return rootView;
        }
    }
    

    PS : the onItemClickListener in the class is for another ListView which is not the one in the navigation drawer. Basically I'm using parse.com and populating a ListView with items from the db.

    Thanks.

    EDIT: I'm finally using this code within onNavigationDrawerItemSelected:

    Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                break;
            case 1:
                fragment = new SearchFragment();
                break;
            default:
                break;
        }
    
        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment).commit();
    
        }
    

    In fragment = new HomeFragment();, android studio tell me that the android.support.v4.app.fragment is needed. Why is that ?

  • ArnaudToutain
    ArnaudToutain almost 10 years
    Thanks for the answer! So in the onNavigationDrawerItemSelected method, I should just use a switch case ? And replace in : fragmentManager.beginTransaction() .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) .commit(); the PlaceholderFragment with anything I want ?
  • ArnaudToutain
    ArnaudToutain almost 10 years
    When creating my Fragment , what should I use considering I'm using the drawer from Android Studio ? android.app.Fragment or android.support.v4.app.Fragment ?
  • Andriy Bas
    Andriy Bas almost 10 years
    better use android.app.Fragment, though another variant is also possible (with some changes)
  • ArnaudToutain
    ArnaudToutain almost 10 years
    I have some problem not using de support.v4. See the edit in my question.
  • Andriy Bas
    Andriy Bas almost 10 years
    use getFragmentManager() instead of getSupportFragmentManager()
  • ArnaudToutain
    ArnaudToutain almost 10 years
    And the very last question: If I want a default DrawerItem to be displayed, how should I do since I dont have access to savedInstanceState ? Thank you very much for everything
  • Andriy Bas
    Andriy Bas almost 10 years
    do you mean you want to display certain Fragment as default when the app is opened ? If yes, there is call selectItem(mCurrentSelectedPosition); in onCreate of NavigationDrawerFragment, it determines which DrawerItem is active by default, replace it with, for example selectItem(1) if you want secondFragment be active on app opening
  • ArnaudToutain
    ArnaudToutain almost 10 years
    Thx a lot for everything. That's perfect.