Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Change fragment layout in runtime

I would like to implement a Single-Activity Multi-Fragments design in my app. I plan to have several "screens" (layouts of Fragment) that I'll switch between (possibly adding to back-stack) in code.

To my understanding the layout of the fragments in each screen is set using Layout objects (e.g. FrameLayout), which act as placeholders for the fragments (sharing the same ID). Since different screens have different Fragment arrangements (one could be FrameLayout, and another LinearLayout, etc.) I was wondering: How do I switch between layouts of fragments in runtime?

I understand adding / replacing Fragments (via FragmentManager), but I'd like to completely add a new layout that contains them, within a live activity. Kind of like having transactions for "setContentView"...

How do I do this? Thanks! Danny.

like image 367
DannyA Avatar asked Oct 23 '11 09:10

DannyA


People also ask

How to change fragments in android?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

How do I inflate fragment layout?

Android calls the onCreateView() callback method to display a Fragment . Override this method to inflate the layout for a Fragment , and return a View that is the root of the layout for the Fragment . The container parameter passed to onCreateView() is the parent ViewGroup from the Activity layout.

What is dynamic fragment?

A fragment is usually used as part of an activity's user interface and contributes its own layout to the activity. A fragment is implemented as independent object -- independent of the activity that contains it. The benefit is that it can be used by multiple activities associated with the application.


1 Answers

It's certainly possible, the only thing you need to do is to generate your own IDs. The IDs may be anything but they must not conflict with the aapt IDs (the ones in R) and must not be negative.

The following example demonstrates this with a set of fixed IDs:

public class MainActivity extends Activity {
    private final int ID_TABLE = 0xA;
    private final int ID_ROW1 = 0xB;
    private final int ID_ROW2 = 0xC;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout ll = (LinearLayout) findViewById(R.id.root);
        TableLayout tl = new TableLayout(this);
        tl.setId(ID_TABLE);
        TableRow tr1 = new TableRow(this);
        tr1.setId(ID_ROW1);
        TableRow tr2 = new TableRow(this);
        tr2.setId(ID_ROW2);
        tl.addView(tr1);
        tl.addView(tr2);
        ll.addView(tl);

        MyFragment frag1 = new MyFragment();
        MyFragment frag2 = new MyFragment();
        MyFragment frag3 = new MyFragment();
        MyFragment frag4 = new MyFragment();

        getFragmentManager().beginTransaction()
            .add(ID_ROW1, frag1, "cell1_1")
            .add(ID_ROW1, frag2, "cell1_2")
            .add(ID_ROW2, frag3, "cell2_1")
            .add(ID_ROW2, frag4, "cell2_2")
            .commit();
        getFragmentManager().executePendingTransactions();
    }
}

In order to switch to a different layout, you can remove the fragments and add them elsewhere.
Let me know how it goes.

EDIT: to clarify, Views and ViewGroups don't need to be instantiated once and then kept for the lifetime of the Activity. Just make sure any fragments are either removed or detached before removing their associated view. Also, if you create and remove views outside of onCreate you should make sure it can be restored by using onSaveInstanceState and repeating the process in onCreate. Read the diagram here and the paragraph about configuration changes.

like image 60
user999717 Avatar answered Sep 20 '22 11:09

user999717