Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the same as the selected answer, except that I've NOT used the support libs. </p> <hr> <p>There are two tabs A and B. </p> <blockquote> <p>Tab A contains Apples and Apricots</p> <p>Tab B contains Bananas and Berries</p> </blockquote> <hr> <p>I've use a FrameLayout as a container to hold these babies </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;FrameLayout android:id="@+id/container_widgets" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>MultiFragsInTabsJelly.java - handles the attach and detach ops. </p> <pre><code>import java.util.ArrayList; import java.util.Iterator; import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.app.ActionBar.Tab; import android.os.Bundle; import android.util.Log; public class MultiFragsInTabsJelly extends Activity { public static String TAG = "EPE"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setup action bar for tabs setContentView(R.layout.main_layout_jelly); ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayShowTitleEnabled(false); actionBar.setTitle("A and B"); Tab tabA = actionBar .newTab() .setText("A"); tabA.setTabListener(new TabAListener(this)); actionBar.addTab(tabA); Tab tabB = actionBar .newTab() .setText("B"); tabB.setTabListener(new TabBListener(this)); actionBar.addTab(tabB); } public static class TabAListener implements ActionBar.TabListener { // FIXME: is this really needed? private static final String appleTag = "apple"; private static final String apricotTag = "apricot"; private final Activity mActivity; private ArrayList&lt;Fragment&gt; fragList; public TabAListener(Activity activity) { mActivity = activity; fragList = null; } public void onTabReselected(Tab tab, FragmentTransaction ft) { // Reselected don't do anything Log.d(TAG, "Tab A: on Tab reselected"); } public void onTabSelected(Tab tab, FragmentTransaction ft) { Log.d(TAG, "Tab A: on Tab Selected"); // attach all the fragments if(fragList == null) { fragList = new ArrayList&lt;Fragment&gt;(); RecordingFragment appleFrag = new RecordingFragment(); ApricotFragment apricotFrag = new ApricotFragment(); ft.add(R.id.container_widgets, appleFrag, appleTag); ft.add(R.id.container_widgets, apricotFrag, apricotTag); fragList.add(appleFrag); fragList.add(apricotFrag); Log.d(TAG, "Tab A: Added fragments to the ArrayList"); } else { Iterator iter = fragList.iterator(); while (iter.hasNext()) { Log.d(TAG, "Tab A: Attaching fragments"); ft.attach((Fragment) iter.next()); } } } public void onTabUnselected(Tab tab, FragmentTransaction ft) { Log.d(TAG, "Tab A: on Tab Unselected"); if(fragList != null) { Iterator iter = fragList.iterator(); while (iter.hasNext()) { Log.d(TAG, "Tab A: Fragments detached"); ft.detach((Fragment) iter.next()); } } } } public static class TabBListener implements ActionBar.TabListener { // FIXME: is this really needed? private static final String bananaTag = "banana"; private static final String berryTag = "berry"; private final Activity mActivity; private ArrayList&lt;Fragment&gt; fragList; public TabBListener(Activity activity) { mActivity = activity; fragList = null; } public void onTabReselected(Tab tab, FragmentTransaction ft) { // Reselected don't do anything Log.d(TAG, "Tab B: on Tab reselected"); } public void onTabSelected(Tab tab, FragmentTransaction ft) { Log.d(TAG, "Tab B: on Tab Selected"); // attach all the fragments if (fragList == null) { fragList = new ArrayList&lt;Fragment&gt;(); BananaFragment bananaFrag = new BananaFragment(); BerryFragment berryFrag = new BerryFragment(); ft.add(R.id.container_widgets, bananaFrag, bananaTag); ft.add(R.id.container_widgets, berryFrag, berryTag); fragList.add(bananaFrag); fragList.add(berryFrag); Log.d(TAG, "Tab B: Added fragments to the ArrayList"); } else { Iterator iter = fragList.iterator(); while (iter.hasNext()) { Log.d(TAG, "Tab B: Attaching fragments"); ft.attach((Fragment) iter.next()); } } } public void onTabUnselected(Tab tab, FragmentTransaction ft) { Log.d(TAG, "Tab B: on Tab Unselected"); if (fragList != null) { Iterator iter = fragList.iterator(); while (iter.hasNext()) { Log.d(TAG, "Tab B: Fragments detached"); ft.detach((Fragment) iter.next()); } } } } } </code></pre> <hr> <p>Here we have the (empty) body of each Fragment. I've only shown one <code>ApricotFragment</code> </p> <p>apricot_fragment.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:layout_width="200dip" android:layout_height="wrap_content" android:padding="6dip" android:text="Apricot" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>ApricotFragment.java</p> <pre><code>import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class ApricotFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.apricot_fragment, container, false); } } </code></pre> <p>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 <a href="http://chat.stackoverflow.com/search?q=tumblr&amp;user=2232873&amp;room=15">to excellent pictures of cats</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload