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)
This is the same as the selected answer, except that I've NOT used the support libs.
There are two tabs A and B.
I've use a FrameLayout as a container to hold these babies
MultiFragsInTabsJelly.java - handles the attach and detach ops.
Here we have the (empty) body of each Fragment. I've only shown one
ApricotFragment
apricot_fragment.xml
ApricotFragment.java
I've you have managed to implement this give yourself a pat on the butt and get some cinnamon rolls with coffee and treat yourself to excellent pictures of cats
Here is my solution:
So obviously after all that, we have to look at how a fragment is made (including inflating a separate layout file). Basically in the onCreateView() method of each fragment, you have it return inflater.inflate(R.layout.THISFRAGMENTSLAYOUT, container, false); Here is the code: