简体   繁体   中英

Switch Fragment with default Android Studio navigation drawer

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 ?

(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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM