Android的:如何在另一个片段添加到主活动 [英] Android: how to add another fragment to the main activity

查看:227
本文介绍了Android的:如何在另一个片段添加到主活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问一个关于如何添加问题上片段包含绘制的东西使用的OpenGL ES
<一href=\"http://stackoverflow.com/questions/24886561/android-opengl-es-2-how-to-use-an-opengl-activity-as-a-fragment-in-the-main-act\">here.有人还跟回答我,但是今天不幸的是我遇到了另一个问题。正如我在其他问题中提到,我的目的是要添加其他片段旁边包含OpenGL和因为我在Android开发初学者我似乎没有做一个明白这是怎么完成的。

I asked a question about how to add a Fragment that contained something drawn using OpenGL ES here. Someone was kind enough to answer that for me, but unfortunately today I encountered another problem. As I mentioned in my other question, my purpose is to add other Fragments next to the one that contains OpenGL and because I am a beginner in Android development I don't seem to understand how this is done.

下面是我想:现在,我的code是完全一个来自我的其他问题。我也有这个片段

Here's what I want: right now, my code is exactly the one from my other question. I also have this 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;
    }
}

与它的布局在一起:

together with its layout:

<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 片段。这是我的主要活动:

and I want to add this to my main activity, where right now I only have the OpenGL Fragment. Here's my main 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();

        }
    }
}

片段的有OpenGL在它和我已经加入到主要活动:

and the Fragment that has OpenGL in it and that I have already added to the main 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;
}


}

我尝试和失败:使用另一个调用。新增方法内 getSupportFragmentManager()或适应这种为code位我的第二个片段

What I tried and failed: using another call to the .add method inside getSupportFragmentManager() or adapting this bit of code for my second Fragment

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

这给了我一个'前pression预期的错误在添加方法。我尝试添加此构造到我的第二个片段

that gave me an 'expression expected' error in the add method. I tried adding this constructor to my second Fragment

public TextFragment()
{
    super();
}    

,然后里面的添加方法,我把。新增(R.id.frag2,新TextFragment())

这仍然没有奏效。

推荐答案

在为了一个片段动态添加到布局,你需要的是一个容器(如在你的情况,这是研究。 id.main_container )。因此,如果你想添加多个片段,你需要的是多个集装箱,就像这样:

In order to dynamically add a Fragment to a layout, what you need is a container (like in your case, it was R.id.main_container). Thus, if you want to add multiple fragments, what you need is multiple containers, like so:

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

(该片段是从<一个href=\"http://stackoverflow.com/questions/3424904/how-to-split-the-screen-with-two-equal-linearlayouts\">How在屏幕上有两个相等的LinearLayouts分裂?)

和那么你就需要添加两个片段:

And then you would need to add the two Fragments:

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

请注意,在单个活动多个片段,最好不要将其添加到backstack,因为这样你不得不preSS回多少次因为有碎片,并且在这种情况下,它更合理使用活动应用程序的意见或状态之间进行导航,而不是通过更换碎片。

Please note that with multiple Fragments on a single Activity, it's better not to add them to the backstack, because then you'd have to press Back as many times as there are Fragments, and in this case it's more reasonable to navigate between the "views" or states of the application with Activities, and not by replacing the Fragments.

(考虑backstack不改变,我不认为backstack监听器需要被删除,但这样做了,这样,如果你preSS回,你不结束活动,但片段中首先,如果你让他们加入到backstack。但是,当它不包含任何片段的活动并没有结束,而且你有一个空观,因此为什么加入。)

(considering the backstack doesn't change, I don't think the backstack listener needs to be removed, but that's done so that if you press Back, you don't end the Activity, but the Fragments within it first if you have them added to the backstack. But the Activity doesn't end when it contains no fragments, and you'd have an "empty view", hence why that was added.)

也请检查旋转工作,甚至在活动后重建数据被保留,因为有你需要设置保留实例状态为true明确的片段为工作的机会。

Please also check if the rotation works and data is maintained even after the activity reconstruction, because there's a chance you need to set the retain instance state to true explicitly on the Fragments for that to work.

这篇关于Android的:如何在另一个片段添加到主活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆