Programmatically add fragments to viewgroup

Killerpixler picture Killerpixler · Mar 1, 2012 · Viewed 25.5k times · Source

basically this is my app(idea) for a tablet landscape orientation: Two Fragments, left fragment is a listfragment populated by a resource.xml file (Got that working).

Right fragment is supposed to dynamically change fragment and layout based on which list item the user clicks. Googling so far told me that I need to programmatically add and remove fragments to a viewgroup to do that. Is that right?

Basically the question is/are:

  1. How do I create the viewgroup and where (Main.java or menufragment.java)?
  2. How do I put the dynamic "user clicked ID 3 on the list so add fragment 3 to the viewgroup"
  3. What do I add to my main.xml file? got the fragment for the listfragment in there, what to add for the dynamic viewgroup?

EDIT:

So this is what my activity looks like: Main.java

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
}

This is my listfragment MenuFragment.java

public class MenuFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.listfragment, container, false);

}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.listmenu)));
    }    



    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Main activity = (Main) getActivity();
    }


}

and finally my main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/list"
    android:layout_width="200dp"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_weight="1"
    class="com.mwerner.fragmentstest.MainMenu" />

<View
    android:id="@+id/contentview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/list" />

</RelativeLayout>

The string array I have in my xml file that populates the list is called "listmenu"

Please tell me where I need to put in the code you wrote down?

Answer

twaddington picture twaddington · Mar 1, 2012

Take a look at these Fragment topics:

Essentially you'll want to have the left fragment tell the parent Activity which item is selected. Then, the Activity can add/remove the correct fragment in the right pane.

Keep in mind that creating/destroying a Fragment is a lot of work for the system. If you can get away with having a single Fragment in the right pane, it will be much more efficient. You can then call methods on that one Fragment instance without building new Fragments.

EDIT (example):

Your Main Activity implementation with custom method:

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    /** Called by the left fragment */
    public void updateRightPane(long id) {
        // Get the data with the selected item id
        // ...

        // Create a new fragment
        MyFragment fragment = new MyFragment(data);

        // Update the layout
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        // The id specified here identifies which ViewGroup to
        // append the Fragment to.
        ft.add(R.id.view_group_id, fragment);
        ft.commit();
    }
}

The onListItemClick implementation for MenuFragment.java:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // Get the parent Activity
    Main activity = (Main) getActivity();

    // Pass the selected item id to the Activity method
    // Note: Feel free to update this method to accept additional
    //       arguments (e.g. position).
    activity.updateRightPane(id);
}

Finally, your layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_group_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <!-- More views here... -->

</RelativeLayout>