简体   繁体   English

使用导航抽屉时可以加载fragment.java文件

[英]possible to load fragment.java file when using navigation drawer

I am just wondering if it is possible to load a fragment.java file into the MainActivity that holds all the navigation drawer code. 我只是想知道是否可以将fragment.java文件加载到保存所有导航抽屉代码的MainActivity中。 I will include code snippets to better explain myself. 我将包含代码片段,以更好地自我介绍。

MainActivity.java MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    switch(position){
    case 0:
     fragmentManager.beginTransaction()
            .replace(R.id.container, SuikodenFragment.newInstance(position + 1)).commit();
     break;
    case 1:
        fragmentManager.beginTransaction()
            .replace(R.id.container, SuikodenIIFragment.newInstance(position + 1)).commit();
        break;
    case 2:
        fragmentManager.beginTransaction()
            .replace(R.id.container, SuikodenIIIFragment.newInstance(position + 1)).commit();
        break;
    case 3:
        fragmentManager.beginTransaction()
            .replace(R.id.container, SuikodenIVFragment.newInstance(position + 1)).commit();
        break;
    case 4:
        fragmentManager.beginTransaction()
            .replace(R.id.container, SuikodenVFragment.newInstance(position + 1)).commit();
        break;
    //similar for others
    }
}

The fragments in this code are also in MainActivity.java and they work. 该代码中的片段也位于MainActivity.java中,并且可以正常工作。 However, what I want to do is link a fragment.java file for each of these cases instead of the inline fragments listed here. 但是,我想针对每种情况链接一个fragment.java文件,而不是此处列出的内联片段。 The reason for this is that the fragment.java file will contain an Expandable ListView with Checkbox. 这样做的原因是fragment.java文件将包含带有Checkbox的Expandable ListView。 Therefore, what is the best way to achieve this? 因此,实现此目标的最佳方法是什么? Is there a line of code that will allow me load a fragment.java file? 是否有一行代码可以加载fragment.java文件?

Yes, it is possible. 对的,这是可能的。

You use the exact same code you are already using, but you replace SuikodenVFragment with the name of the Fragment class you want to use instead. 您使用与已经使用的完全相同的代码,但是将SuikodenVFragment替换为要使用的Fragment类的名称。

Say your project structure looks like this: 假设您的项目结构如下所示:

/com/example/myapp
  MainActivity.java
  SecondFragment.java

Then you can swap in an instance of SecondFragment by calling: 然后,您可以调用以下SecondFragment来交换SecondFragment的实例:

fragmentManager.beginTransaction().replace(R.id.container,
    SecondFragment.newInstance() // or new SecondFragment(), as appropriate
).commit();

The only "catch" is that you need to make sure that you import SecondFragment at the top of MainActivity.java with the rest of your imports. 唯一的“陷阱”是您需要确保在MainActivity.java的顶部导入SecondFragment ,并确保其余的导入。 However, most IDEs will take care of this for you. 但是,大多数IDE都会为您解决这一问题。

If you are new to Java and haven't used an import statement or packages before, I recommend starting with the Using Package Members guide from Oracle's Java tutorials. 如果您是Java的新手,并且以前从未使用过import语句或软件包,建议您从Oracle Java教程的“ 使用软件包成员”指南开始。

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

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