Android:如何向主 Activity 添加另一个 fragment

标签 android android-fragments android-fragmentactivity

我问了一个关于如何添加 Fragment 的问题包含使用 OpenGL ES 绘制的内容 here .好心人帮我解答了,可惜今天又遇到了一个问题。正如我在其他问题中提到的,我的目的是添加其他 Fragments在包含 OpenGL 的那个旁边,因为我是 Android 开发的初学者,所以我似乎不明白这是如何完成的。

这就是我想要的:现在,我的代码正是我的另一个问题中的代码。我也有这个Fragment :

public class TextFragment extends Fragment 
{

    private TextView textview;

    @Override
    public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) 
    {

        View view = inflater.inflate(R.layout.text_fragment,
            container, false);

        textview = (TextView) view.findViewById(R.id.textView1);

        return view;
    }
}

与其布局一起:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frag2">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Fragment Two"
    android:textAppearance="?android:attr/textAppearanceLarge" />
 </RelativeLayout>

我想将它添加到我的主要 Activity 中,现在我只有 OpenGL Fragment .这是我的主要 Activity :

public class FragmentExampleActivity extends FragmentActivity implements ToolbarFragment.ToolbarListener 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener()
        {
            public void onBackStackChanged()
            {
                int backCount = getSupportFragmentManager().getBackStackEntryCount();
                if (backCount == 0)
                {
                    finish();
                }
            }
        });

        if (savedInstanceState == null)
        {
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.main_container, new OpenGLES20ActivityFrag())
                    .addToBackStack(null)
                    .commit();

        }
    }
}

Fragment其中包含 OpenGL,并且我已经将其添加到主要 Activity 中:

public class OpenGLES20ActivityFrag extends Fragment
{
private GLSurfaceView mGLView;

public OpenGLES20ActivityFrag()
{
    super();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    mGLView = new MyGLSurfaceView(this.getActivity()); 
    return mGLView;
}


}

我尝试过但失败了:使用另一个调用 .add里面的方法getSupportFragmentManager()或者为我的第二个 Fragment 修改这段代码

getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.frag2, TextFragment)
                .addToBackStack(null)
                .commit();

这在 add 中给了我一个“预期的表达式”错误方法。我尝试将此构造函数添加到我的第二个 Fragment

public TextFragment()
{
    super();
}    

然后在add里面方法我把.add(R.id.frag2, new TextFragment())

还是不行。

最佳答案

为了向布局动态添加 fragment ,您需要一个容器(就像您的情况一样,它是 R.id.main_container)。因此,如果你想添加多个 fragment ,你需要的是多个容器,像这样:

<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent">
  <FrameLayout android:id="@+id/main_container_1" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
  <FrameLayout android:id="@+id/main_container_2" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
</LinearLayout>

(此 fragment 来自 How to split the screen with two equal LinearLayouts? )

然后您需要添加两个 fragment :

if (savedInstanceState == null)
{
    getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.main_container_1, new OpenGLES20ActivityFrag())
            .commit();

    getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.main_container_2, new TextFragment())
            .commit();
}

请注意,如果单个 Activity 上有多个 Fragment,最好不要将它们添加到后台堆栈,因为那样你必须按 Back 的次数与 Fragment 的数量一样多,在这种情况下导航更合理在应用程序的“ View ”或状态与 Activity 之间,而不是通过替换 Fragments。

(考虑到 backstack 没有改变,我认为不需要删除 backstack 监听器,但这样做是为了如果你按下 Back,你不会结束 Activity,但如果您已将它们添加到后台堆栈。但是当 Activity 不包含任何 fragment 时,它不会结束,并且您会有一个“空 View ”,因此添加了它。)

还请检查旋转是否有效,数据是否在 Activity 重建后得到维护,因为您可能需要在 Fragment 上显式地将保留实例状态设置为 true 才能正常工作。

关于Android:如何向主 Activity 添加另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24905272/

相关文章:

android - 我在 Gradle 托管设备设置中会遇到什么问题?

android - 添加 : annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0" 后 list 合并失败

android - 我不能使用声明的 TextView

android - 使用 FragmentActivity 而不是 Fragment 进行 fragment 事务

MacBook M1 Pro 上的 Android Studio 安装错误 : java. nio.file.AccessDeniedException

java - 在安卓上使用服务加载器

java - Android:切换到 fragment 内的新 fragment ?

android - 关于 inflater.inflate Android 文档的混淆

android - 添加 navGraph 属性时,膨胀类 Fragment 时出错

java - Android,在DIALOG中点击 "ok"后重新加载或刷新当前 fragment