Skip to content

Instantly share code, notes, and snippets.

@nesquena
Last active December 22, 2015 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesquena/0f0a1f50a2a64721309e to your computer and use it in GitHub Desktop.
Save nesquena/0f0a1f50a2a64721309e to your computer and use it in GitHub Desktop.
Android ActionBarSherlock Tab Listener for Fragments
package com.example.sherlockfragmentsdemo;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class SherlockTabListener<T extends SherlockFragment> implements TabListener {
private SherlockFragment mFragment;
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final int mfragmentContainerId;
private final Bundle mfragmentArgs;
// This version defaults to replacing the entire activity content area
// new SherlockTabListener<SomeFragment>(this, "first", SomeFragment.class))
public SherlockTabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
mfragmentContainerId = android.R.id.content;
mfragmentArgs = new Bundle();
}
// This version supports specifying the container to replace with fragment content
// new SherlockTabListener<SomeFragment>(R.id.flContent, this, "first", SomeFragment.class))
public SherlockTabListener(int fragmentContainerId, SherlockFragmentActivity activity,
String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
mfragmentContainerId = fragmentContainerId;
mfragmentArgs = new Bundle();
}
// This version supports specifying the container to replace with fragment content and fragment args
// new SherlockTabListener<SomeFragment>(R.id.flContent, this, "first", SomeFragment.class, myFragmentArgs))
public SherlockTabListener(int fragmentContainerId, SherlockFragmentActivity activity,
String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mfragmentContainerId = fragmentContainerId;
mfragmentArgs = args;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = (SherlockFragment) SherlockFragment
.instantiate(mActivity, mClass.getName(), mfragmentArgs);
ft.add(mfragmentContainerId, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment