6

I'm developing an android application making use of fragments which is more of a Master/Detail form. I want the main activity to consist of a list fragment on the left side, based on the item chosen on the left i want to display a fragment with different layout on the right side. (Note: each fragment on the right require different layouts/views)

All the examples that I've come across make use of only one common fragment on the right by changing some values in it or swapping/replacing new fragments having same layout.

If someone could shed some light on this problem then it will help me immensely. Thanks!

mervyn.777
  • 107
  • 2
  • 8

1 Answers1

9

If you are using framelayouts to hold your fragments, it's the same as those other you mention. You just instantiate your fragment (whatever the layout) and swap it into the framelayout in place of the other one.

If you have hardcoded your fragments into the XML, you won't be able to do that (as far as I've been able to determine).

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frames"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/hline1"
    android:layout_below="@id/horizontalline"
    android:orientation="horizontal" >
    <FrameLayout
        android:id="@+id/leftpane"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight=".4" />
    <TextView
        android:id="@+id/verticalline"
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="@color/bar_background"
        android:gravity="center_horizontal"
        android:paddingLeft="5dip"
        android:paddingRight="5dip" />
    <FrameLayout
        android:id="@+id/rightpane"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </FrameLayout>
</LinearLayout>

Then you use the id for the framelayout and the name of your instantiated fragment to put your fragment into the framelayout.

EventListFragment eventlist = new EventListFragment();
getFragmentManager().beginTransaction().replace(R.id.leftpane, eventlist).commit();

EventDetailFragment eventadd = new EventDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventadd).commit();

When you want to change the contents, you do the same thing again (the following would replace the fragment in the right pane with a new/different fragment, which can have it's own, different, layout associated with it):

EventSuperDetailFragment eventsuper = new EventSuperDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventsuper).commit();
Barak
  • 16,318
  • 9
  • 52
  • 84
  • Thanks a lot for your help! Fragment concept is quite new to me. Could you illustrate your point with some simple example? – mervyn.777 Aug 03 '12 at 14:09
  • I've updated my answer, hopefully it'll point you in the right direction. Let me know! – Barak Aug 03 '12 at 14:41
  • Thanks Barak!!! Yes, it worked! Also I wanted to know if its possible to create a fragment within a fragment. – mervyn.777 Aug 07 '12 at 12:47