I am developing an Android app with fragments that has many possible flows. With simple Activity-based flow, it can flow like this:

A -> B -> E
A -> C -> E
A -> D -> E

With fragments, I would like to be able to do this same thing with a screen split into thirds. The question that I am asking is: in my layout XML file, how do I represent a space that may be filled with one of three different fragments?

有帮助吗?

解决方案

Take a look at FragmentTransaction.add() function:

public abstract FragmentTransaction add (int containerViewId, ... )
...
containerViewId - Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container.

In other words, just create any empty GroupView in your xml layout and pass id of this group into this function when adding the fragment. Then remove it when you don't want it anymore.

其他提示

If you won't need to replace fragments in your activity, you can put the fragment declaration directly inside the XML.

<fragment
    android:name="com.mypackage.ui.MyFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myfragment_Fragment">
</fragment>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top