【问题标题】:Android: how to add another fragment to the main activityAndroid:如何向主活动添加另一个片段
【发布时间】:2014-07-23 18:45:42
【问题描述】:

我问了一个关于如何添加包含使用 OpenGL ES 绘制的内容的 Fragment 的问题 here。有人很好地为我回答了这个问题,但不幸的是今天我遇到了另一个问题。正如我在另一个问题中提到的那样,我的目的是在包含 OpenGL 的那个旁边添加其他 Fragments,因为我是 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>

我想将此添加到我的主要活动中,现在我只有 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();

        }
    }
}

还有 Fragment 里面有 OpenGL 并且我已经添加到主要活动中:

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


}

我尝试过但失败了:在getSupportFragmentManager() 中使用另一个对.add 方法的调用,或者为我的第二个Fragment 调整这段代码

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

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

public TextFragment()
{
    super();
}    

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

还是不行。

【问题讨论】:

  • 你应该看到一个教程来学习如何将两个片段放在一起,比如这个:javacodegeeks.com/2013/06/…
  • private static TextView textview; 不要将 TextView 设为静态。
  • 添加到@Zhuinden 这将造成上下文泄漏。静态可变变量在 Java 中也是不好的做法
  • 好的我删除了TextView的静态声明,我会尝试@Zhuinden提供的解决方案并提供反馈

标签: android android-fragments android-fragmentactivity


【解决方案1】:

为了动态地将片段添加到布局中,您需要一个容器(就像您的情况一样,它是 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>

(这个sn-p来自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 上有多个 Fragment 时,最好不要将它们添加到 backstack,因为这样您就必须按 Fragment 多次按 Back,在这种情况下导航更合理在应用程序的“视图”或状态之间使用活动,而不是通过替换片段。

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

还请检查轮换是否有效,并且即使在活动重建之后也能维护数据,因为您可能需要在 Fragments 上将保留实例状态显式设置为 true 才能正常工作。

【讨论】:

    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多