如何使用FragmentManager显示片段A,B,C,...在一个的FrameLayout? [英] How can I use FragmentManager to display Fragment A, B, C,... in a FrameLayout?

查看:196
本文介绍了如何使用FragmentManager显示片段A,B,C,...在一个的FrameLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的StackOverflow的人,

Dear StackOverflow people,

我在我的Andr​​oid应用程序目前有一个很大的问题。

I have currently one big issue in my Android application.

我使用的片段我所有的活动,我看这里的所有文档:<一href="http://developer.android.com/guide/topics/fundamentals/fragments.html">http://developer.android.com/guide/topics/fundamentals/fragments.html

I am using Fragments for all my activities and I read all the doc here: http://developer.android.com/guide/topics/fundamentals/fragments.html

我的应用程序现在已经在手机和平​​板电脑一个非常酷的样子。

My application has now a very cool look on phones and tablets.

我的问题:

予有显示包括在一个的FrameLayout一个碎片的布局(见截图在底部)

I have a layout displaying a Fragments that is included in a FrameLayout (see screenshot at the bottom)

所以,这是一个片段截图。

So, this is a screenshot with Fragment A.

现在,我想在左键点击的时候,更换片段A和片段B,或C,或...

Now, I would like when clicking on a left button, to replace the Fragment A with Fragment B, or C, or...

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.stackoverflow.question.FRAGMENT_A"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</FrameLayout>

正如你在我的XML看,FRAGMENT_A很难codeD。

As you can see in my xml, FRAGMENT_A is hardcoded.

想象一下,我想6个不同的片段之间进行切换,什么shoulmd怎么办? 把我所有的6个片段中的XML,或者是有没有办法来取代编程FRAGMENT_A与FRAGMENT_B,C,D,E等。

Imagine I want to switch between 6 different fragments, what shoulmd I do? Put all my 6 Fragments in the XML, or is there a way to replace programmatically FRAGMENT_A with FRAGMENT_B, C, D , E, etc.

感谢了很多的帮助。

推荐答案

使用FragmentTransaction取代片段。

use FragmentTransaction to replace fragments.

您可以做片段的替代编程。

you can do the replace of fragments programatically.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
FragmentB fb = new FragmentB();
ft.replace(R.id.list, fb);
ft.addToBackStack("replacingFragmentA");
ft.commit();

添加到背堆栈是可选的。

add to back stack is optional.

这篇关于如何使用FragmentManager显示片段A,B,C,...在一个的FrameLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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