Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap fragments with different layouts in android?

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!

like image 646
mervyn.777 Avatar asked Aug 02 '12 23:08

mervyn.777


People also ask

How do I switch between fragments?

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 you specify the fragment layout inside the activity code?

You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Is it possible to reuse a fragment in multiple screens?

Yes you can! Show activity on this post.


1 Answers

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();
like image 107
Barak Avatar answered Oct 21 '22 13:10

Barak