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

1

My question is:

  • Is it possible to use two layouts in the same Fragment?

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

I wanted a feature of this genre where the user would fill out a form in several steps. To develop, is there any way to use multiple layouts in the same fragment or for each step of the configuration is it necessary to use multiple fragments and their layouts?

    
asked by anonymous 24.02.2017 / 02:46

2 answers

1

One alternative is to use include to include another layout in your FrameLayout . The logic is this, you leave a hidden layout and the other the view. This way, the moment you click the Next button, you hide the layout from the view and show 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);
}

This would be practically a base, depending on what you are going to do, you can adapt very well.

    
24.02.2017 / 03:19
3

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

Create a layout for each part of the form.

In the Activity / fragment layout where you want to display 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>

For 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 ViewStub maintaining the hierarchy of lighter views , since layouts are only added to the hierarchy after "inflated."

    
24.02.2017 / 12:09