문제

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