简体   繁体   English

使用默认的Android Studio导航抽屉切换片段

[英]Switch Fragment with default Android Studio navigation drawer

I'm a little bit lost with the implementation of the navigation drawer in Android Studio. 我在Android Studio中实现导航抽屉时有点迷失。 The onCreate method call the PlaceholderFragment class which I don't really understand what is it for. onCreate方法调用PlaceholderFragment类,我真的不明白它是什么。

But anyway, where should I implement my onItemClickListener to display different fragments according to the item selected in the navigation drawer ? 但无论如何,我应该在哪里实现我的onItemClickListener根据导航抽屉中选择的项目显示不同的fragments

This is my current PlaceholderFragment : 这是我目前的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. PS:类中的onItemClickListener用于另一个ListView ,它不是导航抽屉中的那个。 Basically I'm using parse.com and populating a ListView with items from the db. 基本上我正在使用parse.com并使用db中的项填充ListView

Thanks. 谢谢。

EDIT: I'm finally using this code within onNavigationDrawerItemSelected : 编辑:我终于在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(); fragment = new HomeFragment(); , android studio tell me that the android.support.v4.app.fragment is needed. ,android studio告诉我需要android.support.v4.app.fragment。 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. (如果你没有这样做):在AndroidStudio中,最好使用导航抽屉生成示例活动(Alt +插入 - >活动 - >导航抽屉活动(在Android Studio 0.8。+上))您将获得活动使用一些drawable托管NavigationDrawerFragment。

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) . 在NavigationDrawerFragment onCreateView您填充要在抽屉中显示的列表项,并且活动将使用方法onNavigationDrawerItemSelected(int position)自动实现NavigationDrawerFragment.NavigationDrawerCallbacks接口。 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. 单击导航抽屉中某个位置的项目时会调用此方法,在此方法中,将R.id.container替换R.id.container位置所需的片段。

You don't have to use PlaceholderFragment , it is the mock to be displayed at first time, before you implement your own. 您不必使用PlaceholderFragment ,它是在您实现自己之前首次显示的模拟。 Feel free to delete it and create any fragment you want. 随意删除它并创建您想要的任何片段。

And OnItemClickListener is already implemented in method onCreateView of NavigationDrawerFragment. OnItemClickListener已经在NavigationDrawerFragment的onCreateView方法中实现。

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. 在实现NavigationDrawerFragment的活动中的onNavigationDrawerItemSeleced(int position)中选择项目时,可以显示片段。 You can use a FragmentManager to swap out the old fragment with the new. 您可以使用FragmentManager将旧片段换成新片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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