referring to this code below (taken from https://gist.github.com/1126843 ) how do i set the contents of the tabs?
public class NativeTabActivity extends Activity {
private TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
addTab(new TextView(this), "Tab 1");
addTab(new TextView(this), "Tab 2");
addTab(new TextView(this), "Tab 3");
}
private void addTab(final View content, final String title) {
View tabView = LayoutInflater.from(this).inflate(R.layout.abs__action_bar_tab_layout, null);
TextView tv = (TextView) tabView.findViewById(R.id.abs__tab);
tv.setText(title);
TabSpec setContent = mTabHost.newTabSpec(title).setIndicator(tabView).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return content;
}
});
mTabHost.addTab(setContent);
}
}
from the code, it seems I need to put the contents under the View createTabContent(String tag) but how do I do it?
I found Rymnel's answer very helpful, but I had to make some changes to get it working properly with ActionBarSherlock 4.0. I was having problems with overriding the onTab methods passing in FragmentTransactions, so I just used the default methods and reassigned the "ft" var inside the method. I'm sure there's a cleaner way to do this, but here is my working code:
public class TabTestActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayHomeAsUpEnabled(true);
bar.setDisplayShowTitleEnabled(true);
bar.setTitle("Activity Title");
bar.addTab(bar
.newTab()
.setText("Tab 1")
.setTabListener(
new TabListener<TabTest1>(this, "tab1",
TabTest1.class, null)));
bar.addTab(bar
.newTab()
.setText("Tab 2")
.setTabListener(
new TabListener<TabTest2>(this, "tab2",
TabTest2.class, null)));
bar.addTab(bar
.newTab()
.setText("Tab 3")
.setTabListener(new TabListener<TabTest3>(this, "tab3", TabTest3.class, null)));
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar()
.getSelectedNavigationIndex());
}
public class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private final FragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private Fragment mFragment;
public TabListener(FragmentActivity activity, String tag, Class<T> clz,
Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
FragmentTransaction ft = mActivity.getSupportFragmentManager()
.beginTransaction();
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getSupportFragmentManager()
.findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
ft.detach(mFragment);
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft = mActivity.getSupportFragmentManager()
.beginTransaction();
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(),
mArgs);
ft.add(android.R.id.content, mFragment, mTag);
ft.commit();
} else {
ft.attach(mFragment);
ft.commit();
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft = mActivity.getSupportFragmentManager()
.beginTransaction();
if (mFragment != null) {
ft.detach(mFragment);
ft.commitAllowingStateLoss();
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With