Villat
Villat

Reputation: 1475

Inflate a layout from an OnClick listener

I have a class that extends from a Fragment, and I want to make a form inside that fragment. For that, I need to inflate differents layouts without moving out of the fragment. (Image for description, and code)

When clicking in "Next", the app must go to the "step 2" BUT keep the "Denunciar" TAB from upside

When clicking in "Next", the app must go to the "step 2" BUT keep on the "Denunciar" tab from upside

I try doing this, but I don't know where is the mistake

public class Tab1 extends Fragment implements View.OnClickListener{

    ViewGroup container;
    LayoutInflater inflater;
    View v;

    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.tab_1,container,false);

        Button buttonTab1 = (Button) v.findViewById(R.id.botonTab1);
        buttonTab1.setOnClickListener(this);

        this.container = container;
        this.inflater = inflater;
        return v;
    }

    @Override
    public void onClick(View v) {
        v = inflater.inflate(R.layout.tab_2,container,false);
    }
}

Upvotes: 0

Views: 1016

Answers (1)

user370305
user370305

Reputation: 109247

First of all is not working because, your fragment only going to attached view to nutshell on onCreateView() method, and once view attached to window then for new View you have to redraw view again. And again If you can track the state of fragments based on condition what you have applied, you can detach and attach Fragment so it will create Fragment again which cause to call onCreateView() again now on some condition based you can change layout file.

To handle this either you can use Nested Fragments or ViewPager inside Fragments.

Some references:

http://developer.android.com/about/versions/android-4.2.html#NestedFragments

https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/Nested

Upvotes: 2

Related Questions