I’ve been trying to update my app and get going with fragments, the
action bar, and all the other UI features that I’m missing out on. I
understand I can have multiple fragments in an activity, have
different layouts based upon the device and all that good stuff but
I’m struggling with getting some tab stuff the way I want. I
understand how to add tabs, switching between them but how do I have
more than one fragment in a tab? So for example I have essentially two
screens I want the user to be able to switch back and forth from
easily (why I want to use tabs). If I have two separate activities I
can specify this in xml files and use setContentView using the layouts
below
tab1_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.example.tabrefactor.Fragment1"
android:id="@+id/fragment_1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<fragment
android:name="com.example.tabrefactor.Fragment2"
android:id="@+id/fragment_2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<fragment
android:name="com.example.tabrefactor.Fragment3"
android:id="@+id/fragment_3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
tab2_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.example.tabrefactor.Fragment4"
android:id="@+id/fragment_4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
I can convert the second layout to using tabs since its only contains
one fragment but I’m not sure how get the first layout into a single
tab. Is that something that’s allowed? Thanks in advance,
Jason Prenger
I’ll leave this open incase someone has a simplification or a better idea…
Eventually went with a workaround of having a base layout with 3 frame layouts…
Then in my activity with the tabs I made a custom TabListener that handled the changes between. The code I used is below (I’m using actionbarsherlock so it’ll look slightly different than the normal stuff)