使用actionbarsherlock标签样式选项卡的内容 [英] tab contents using actionbarsherlock tab style
本文介绍了使用actionbarsherlock标签样式选项卡的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
参照以下(摘自 https://gist.github.com/1126843这code一>)如何设置选项卡的内容是什么?
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);
}
}
从code,看来我需要把内容查看createTabContent(字符串标签)下,但我怎么办呢?
from the code, it seems I need to put the contents under the View createTabContent(String tag) but how do I do it?
推荐答案
我用片段为我的福尔摩斯实施。
I use Fragments for my Sherlock implementation.
public class ActionBarTabs extends FragmentActivity {
@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.addTab(bar.newTab()
.setText("Home")
.setTabListener(new TabListener<DashBoardFragment>(
this, "home", DashBoardFragment.class, null)));
bar.addTab(bar.newTab()
.setText("Inventory")
.setTabListener(new TabListener<InventoryFragment>(
this, "inventory", InventoryFragment.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 Location selection
Intent intent = new Intent(ActionBarTabs.this, LocationSelectorActivity.class);
ActionBarTabs.this.startActivityForResult(intent,0);
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 = 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 = mActivity.getSupportFragmentManager().beginTransaction();
if (mFragment != null) {
ft.detach(mFragment);
ft.commitAllowingStateLoss();
}
}
@Override
public void onTabReselected(Tab tab) {
}
}
}
加载不同的片段插入选项卡中的关键的变化是简单地改变YOUR_FRAGMENT_NAME到你的片段类在这些线路的名称:
The key change to load different fragments into the tabs is to simply change the "YOUR_FRAGMENT_NAME" to the name of your fragment class in these lines:
bar.addTab(bar.newTab()
.setText("Home")
.setTabListener(new TabListener<YOUR_FRAGMENT_NAME>(
this, "home", YOUR_FRAGMENT_NAME.class, null)));
希望这有助于!
Hope this helps!
这篇关于使用actionbarsherlock标签样式选项卡的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文