Java 如何使用 Android 导航抽屉更改片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20638967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:17:07  来源:igfitidea点击:

How to change fragments using Android navigation drawer

javaandroidandroid-fragments

提问by user3053246

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 navigation drawer that has been created AUTOMATICALLY by the app (AndroidStudio)
  • 我创建了一个应用程序并使用了由应用程序(AndroidStudio)自动创建的导航抽屉

Here's what I have:

这是我所拥有的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

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

public void onSectionAttached(int number) {
    switch (number) {
        case 1:

            break;
        case 2:

            break;
        case 3:

            break;
    }
}

And some more here:

还有一些在这里:

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

I want to display another fragment using the button in navigation drawer. I want to use this code so please do not send me any guides or tutorials making their own drawers..

我想使用导航抽屉中的按钮显示另一个片段。我想使用此代码,所以请不要向我发送任何制作自己抽屉的指南或教程..

The question is, what do I put in case 1:case 2:and case 3:in case I want to open another fragment? Thanks.

现在的问题是,我该怎么放case 1:case 2:,并case 3:在情况下,我想打开另一个片段?谢谢。

One more question:

还有一个问题:

  • How do I add more fragments and transactions? This doesn't work-

    Fragment fragment = new MyFragment1();
    Fragment frag = new MyFragment2();
    FragmentManager fragmentManager = getFragmentManager();
    switch(position) {
        case 0:
            fragment = new MyFragment1();
            break;
        case 1:
            frag = new MyFragment2();
    
            break;
    }
    fragmentManager.beginTransaction()
            .replace(R.id.container, fragment).commit();
    
  • 如何添加更多片段和交易?这不起作用-

    Fragment fragment = new MyFragment1();
    Fragment frag = new MyFragment2();
    FragmentManager fragmentManager = getFragmentManager();
    switch(position) {
        case 0:
            fragment = new MyFragment1();
            break;
        case 1:
            frag = new MyFragment2();
    
            break;
    }
    fragmentManager.beginTransaction()
            .replace(R.id.container, fragment).commit();
    

采纳答案by Dreagen

You should just put a switch statement into the onNavigationDrawerItemSelectedmethod.

您应该只在onNavigationDrawerItemSelected方法中放入一个 switch 语句。

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

这只是很快完成,但我认为它应该起作用

回答by Egor

You need to create a switchblock inside the onNavigationDrawerItemSelected() and use the code that's already there for every casebut with corresponding Fragmentinstead of PlaceholderFragment. Now it contains a generic piece of code for adding a PlaceholderFragmentto the layout, reuse it for your purposes.

您需要创建一个switch内部的块onNavigationDrawerItemSelected(),并使用已经存在于每一个代码case,但相应Fragment的代替PlaceholderFragment。现在它包含了一段用于向PlaceholderFragment布局添加 a 的通用代码,可将其重用于您的目的。

回答by DenisGl

I solved this problem over inflater:

我解决了这个问题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;
    }

回答by Nemesis

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

DenisGl 的回应,是正确的方法!!!使用默认创建的类成员,您可以在导航抽屉的各个组件之间切换!!您必须使用 onCreateView 方法,该方法位于类成员 PlaceholderFragment 中。该类将在方法 onNavigationDrawerItemSelected 中自动调用

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

下面是代码示例: /这个方法可以保持原样!它会自动调用类 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:

而是使用此方法,您可以在其中输入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!

这 100% 有效,测试验证!

回答by user5157325

It does work in Eclipse IDE

它确实在 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;
}