Android:如何将另一个片段添加到主活动中

时间:2014-07-23 08:19:54

标签: android android-fragments android-fragmentactivity

我问了一个关于如何添加包含使用OpenGL ES绘制的内容的Fragment的问题 here。对我来说,有人很友好地回答了这个问题,但不幸的是,今天我遇到了另一个问题。正如我在其他问题中提到的,我的目的是在包含OpenGL的那个旁边添加其他Fragments,因为我是Android开发的初学者,我似乎不明白这是如何完成的。< / p>

这就是我想要的:现在,我的代码正是我另一个问题中的代码。我也有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>

我想将此添加到我的主要活动中,现在我只有OpenGL Fragment。这是我的主要活动:

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();

        }
    }
}

以及其中包含OpenGL且我已添加到主要活动中的Fragment

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();

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

public TextFragment()
{
    super();
}    

然后在我add

.add(R.id.frag2, new TextFragment())方法内

仍然没有用。

1 个答案:

答案 0 :(得分:2)

为了动态地将片段添加到布局,您需要的是一个容器(就像您的情况一样,它是R.id.main_container)。因此,如果要添加多个片段,您需要的是多个容器,如下所示:

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

(此代码段来自How to split the screen with two equal LinearLayouts?

然后你需要添加两个片段:

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上的多个片段,最好不要将它们添加到Backstack中,因为那时你必须按回到片段的次数,在这种情况下导航更合理在“视图”或具有活动的应用程序状态之间,而不是替换碎片。

(考虑到backstack没有改变,我认为不需要删除backstack监听器,但是这样做是为了如果你按Back,你不会结束Activity,但是如果你在你把它们添加到了backstack。但是当Activity不包含任何片段时,它不会结束,你会有一个“空视图”,因此为什么会添加它。)

请检查旋转是否有效以及数据是否在活动重建后仍然保持,因为您可能需要在Fragments上明确将retain实例状态设置为true才能生效。

相关问题