Permalink
Browse files

Added fragment support to TabHost

  • Loading branch information...
1 parent 0539a36 commit 46f82c0eb7442627068396d360bfdf9584304730 @scruffyfox committed Mar 7, 2012
View
@@ -4,5 +4,6 @@
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
+ <classpathentry kind="lib" path="lib/android-support-v13.jar"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
@@ -1,5 +1,2 @@
-#Fri Aug 19 16:56:57 BST 2011
eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
-org.eclipse.jdt.core.compiler.compliance=1.5
-org.eclipse.jdt.core.compiler.source=1.5
+org.eclipse.jdt.core.compiler.processAnnotations=enabled
View
Binary file not shown.
View
@@ -11,6 +11,9 @@
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
@@ -184,6 +187,71 @@ public void select(LocalActivityManager activityManager, int targetView)
/**
* Selects the tab
+ * @param fragmentManager The FragmentManager used for initiating fragment views
+ * @param targetView The target container
+ */
+ public Fragment select(FragmentManager fragmentManager, Fragment lastFragment, int targetView)
+ {
+ if (isSelected) return lastFragment;
+
+ // Start the new intent
+ if (fragmentManager == null)
+ {
+ Log.e("XUITab", "No fragment manager");
+ select();
+ return null;
+ }
+
+ if (targetView < 1)
+ {
+ Log.e("XUITab", "No Target view");
+ select();
+ return null;
+ }
+
+ if (this.params.intent == null)
+ {
+ Log.e("XUITab", "No Intent for tab");
+ select();
+ return null;
+ }
+
+ try
+ {
+ Class<?> fragmentClass = Class.forName(this.params.intent.getComponent().getClassName());
+
+ if (!(fragmentClass.getSuperclass().equals(Fragment.class)))
+ {
+ throw new IllegalArgumentException("Class does not extend Fragment");
+ }
+
+ Fragment clazz = Fragment.instantiate(context, fragmentClass.getName());
+ FragmentTransaction ft = fragmentManager.beginTransaction();
+
+ if (lastFragment != null)
+ {
+ ft.detach(lastFragment);
+ }
+
+ ft.add(targetView, clazz);
+ ft.commit();
+
+ lastFragment = clazz;
+
+ fragmentManager.executePendingTransactions();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+
+ select();
+
+ return lastFragment;
+ }
+
+ /**
+ * Selects the tab
*/
public void select()
{
View
@@ -15,6 +15,8 @@
import android.content.res.TypedArray;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
@@ -59,15 +61,22 @@
* tabHost.addTab(tab, tabParams);
* tabHost.selectTab(0);
* @endcode
+ *
+ * If you are using fragments instead of Activities, you need to extend FragmentActivity and call setup with getSupportFragmentManager();
+ * @code
+ * tabHost.setup(getSupportFragmentManager());
+ * @endcode
*/
public class XUITabHost extends RelativeLayout
{
private Context mContext;
private int mTotalChildren;
private int mTargetView = -1;
private LocalActivityManager mActivityManager;
+ private FragmentManager mFragmentManager;
private OnTabSelectedListener mOnTabSelected;
private Animation mContentAnimation;
+ private Fragment lastFragment = null;
/**
* Default constructor
@@ -112,14 +121,33 @@ public void setOnTabSelectedListener(OnTabSelectedListener listener)
*/
public void setup(LocalActivityManager activityManager)
{
+ if (mFragmentManager != null)
+ {
+ throw new IllegalArgumentException("Setup has already been called before with FragmentManager");
+ }
+
this.mActivityManager = activityManager;
}
/**
+ * Calls to set up the tab host
+ * @param fragmentManager The activity manager from FragmentActivity
+ */
+ public void setup(FragmentManager fragmentManager)
+ {
+ if (mActivityManager != null)
+ {
+ throw new IllegalArgumentException("Setup has already been called before with LocalActivityManager");
+ }
+
+ this.mFragmentManager = fragmentManager;
+ }
+
+ /**
* Selects a tab at a specific index
* @param index The index of the tab
*/
- public void selectTab(final int index)
+ public void selectTab(int index)
{
XUITab tab = (XUITab)getChildAt(index);
tab.setSoundEffectsEnabled(false);
@@ -215,7 +243,7 @@ public int getTabCount()
*/
private class CustomTabClickListener implements OnClickListener
{
- private int index;
+ private int index;
/**
* Default constructor
@@ -232,15 +260,23 @@ public CustomTabClickListener(int index)
*/
public void onClick(View view)
{
- if (((XUITab)view).isSelected()) return;
+ if (((XUITab)view).isSelected()) return;
if (mContentAnimation != null)
{
((XUITab)view).setAnimation(mContentAnimation);
}
deselectAll();
- ((XUITab)view).select(mActivityManager, mTargetView);
+
+ if (mFragmentManager != null)
+ {
+ lastFragment = ((XUITab)view).select(mFragmentManager, lastFragment, mTargetView);
+ }
+ else
+ {
+ ((XUITab)view).select(mActivityManager, mTargetView);
+ }
if (mOnTabSelected != null)
{

0 comments on commit 46f82c0

Please sign in to comment.