monkjack monkjack - 6 months ago 8
Android Question

How do I change the view inside a fragment?

I have a fragment which creates its view inside onCreateView as you'd expect. However I want to change the view periodically.

My use case is that the fragment shows some data from a web service. When a user chooses an option from a list (another fragment), this fragment should switch to a progress bar, then once loaded, switch back to the data view.

I could make two fragments - the loading fragment, and the displaying fragment, but it seems that since this is an encapsulated event I'd rather do it all inside the one fragment.

Basically what I am asking, is what is the equivilent of setContentView inside a fragment.

Answer

You can use Fragment.getView(). This returns the view of the container which contains the Fragment. On that view, you can call removeAllViews. Then build your new views and add them to the view you got from getView().

http://developer.android.com/reference/android/app/Fragment.html#getView()