Two Layouts.xml for the same Fragment, is it possible?

Asked

Viewed 250 times

1

My question is this::

  • It is possible to use two layouts in one Fragment?

The context of my doubt, is the following: I wanted a form, where the user entered several information, only I can not put everything in the same layout, and I thought of the example of the initial configuration of android:

inserir a descrição da imagem aqui

I wanted a feature of this genus where the user, fill out a form in several steps. To develop, there is some way to use multiple layouts in the same Ragment or for each configuration step it is necessary to use multiple Ragments and respective layouts?

2 answers

3

One possibility is to use Viewstub.
Viewstub is a view light without dimensions that does not participate in the layout.

Create a layout for each part of the form.

In the Activity/Ragment layout where you want to submit the form declare a Viewstub for each part:

<FrameLayout ...>
    <ViewStub
        android:id="@+id/stub_parte1"
        android:inflatedId="@+id/frm_parte1"
        android:layout="@layout/formulario_parte1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ViewStub
        android:id="@+id/stub_parte2"
        android:inflatedId="@+id/frm_parte2"
        android:layout="@layout/formulario_parte2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    ...
    ...
</FrameLayout>

To get a reference to each Viewstub use:

stub_parteX = ((ViewStub) findViewById(R.id.stub_parteX));  

To bring to the screen each part of the form use:

View formulario_parteX = stub_parteX.inflate();

To hide a part use:

stub_parteX.setVisibility(View.GONE);

The advantage of using Viewstub over the traditional Hide/show of layouts, is the Viewstub maintain the hierarchy of views lighter, since the layouts are only added to the hierarchy after "inflated".

  • thanks, for the answer, I am testing several possibilities,and in the meantime I have a small error that I am trying to solve, but as soon as I can, I see your solution :)

1


An alternative is to use the include to include another layout in his FrameLayout. The logic is this, you leave one layout hidden and the other the view. From this form, the moment you click the button Next, you hide the layout that is in sight and shows the layout that is hidden. See:

XML

<FrameLayout ...>
    <include 
        android:id="@+id/layout1"
        layout="@layout/linear1"/>
    <include 
        android:id="@+id/layout2"
        layout="@layout/linear2"/>
</FrameLayout>

Fragment

View layout1, layout2;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.list_fragment, container, false);
    layout1 = root.findViewById(R.id.layout1);
    layout2 = root.findViewById(R.id.layout2);

    return root;
}
// aqui seria seu botão, que escondera seu layout1 e mostrará o 2.
public void mostrarLayout2() {
    layout1.setVisibility(GONE);
    layout2.setVisibility(VISIBLE);
}

That would be practically a base, depending on what you’re going to do, you can adapt very well.

  • Thank you for the answer! I will try the answer! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.