我已经在我的项目中成功实现了actionsherlock。这是我的代码:
public class ActiontabActivity extends SherlockFragmentActivity {
ActionMode mMode;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
setContentView(R.layout.main);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayShowHomeEnabled(false);
bar.setDisplayShowTitleEnabled(false);
bar.setTitle("Currency Exchange");
bar.addTab(bar
.newTab()
.setText("Converter")
.setTabListener(
new TabListener<Fragment>(this, "Converter",
ConverterTab.class, null)));
bar.addTab(bar
.newTab()
.setText("Favorites")
.setTabListener(
new TabListener1<Fragment>(this, "Favorites",
FavoritesTab.class, null)));
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
@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<ConverterTab> mClass;
private final Bundle mArgs;
private Fragment mFragment;
public TabListener(FragmentActivity activity, String tag, Class<ConverterTab> 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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
if (mFragment != null) {
//this segment removes the back history of everything in the tab you are leaving so when you click on the tab again you go back to a fresh start
FragmentManager man = mFragment.getFragmentManager();
if(man.getBackStackEntryCount()>0) //this check is required to prevent null point exceptions when clicking off of a tab with no history
man.popBackStack(man.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE); //this pops the stack back to index 0 so you can then detach and then later attach your initial fragment
//also it should be noted that if you do popbackstackimmediate here instead of just popbackstack you will see a flash as the gui changes back to the first fragment when the code executes
//end
ft.detach(mFragment);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
public class TabListener1<T extends Fragment> implements ActionBar.TabListener {
private final FragmentActivity mActivity;
private final String mTag;
private final Class<FavoritesTab> mClass;
private final Bundle mArgs;
private Fragment mFragment;
public TabListener1(FragmentActivity activity, String tag, Class<FavoritesTab> 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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
if (mFragment != null) {
//this segment removes the back history of everything in the tab you are leaving so when you click on the tab again you go back to a fresh start
FragmentManager man = mFragment.getFragmentManager();
if(man.getBackStackEntryCount()>0) //this check is required to prevent null point exceptions when clicking off of a tab with no history
man.popBackStack(man.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE); //this pops the stack back to index 0 so you can then detach and then later attach your initial fragment
//also it should be noted that if you do popbackstackimmediate here instead of just popbackstack you will see a flash as the gui changes back to the first fragment when the code executes
//end
ft.detach(mFragment);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}
我在转换器选项卡中放了一个按钮,在收藏夹选项卡中放了一个复选框。但是现在我想要当我运行我的应用程序时,它最初没有选择任何标签。只显示主屏幕,当我按任何标签时它显示它&#39 ; s contents。因为最初我想在没有选中标签时显示其他内容。请告诉我该怎么做?请告诉我如何才能显示最初的主屏幕然后当我选择任何标签时它会显示其内容我附上了我的项目图片:
答案 0 :(得分:2)
您可以通过以下方式执行此操作:
bar.addTab(yourTab,false);
像这样,选项卡仍然是可视选择的,但是不执行侦听器中onTabSelected方法中的操作。
答案 1 :(得分:0)
不幸的是,当您创建标签时,Android应将任何标签标记为已选中。但我有一些想法。
第一个。您可以替换内容。只需创建一个布尔标志,并在选中选项卡时更改其值。因此,当标志为假(例如)时,您可以显示主屏幕的内容。
或者第二个想法。只需使用选项卡和主屏幕的内容进行另一个主要活动。当选择任何标签时,只需使用真实标签运行您的活动。
对不起我的英文。有些事情可能不清楚。所以问我,我会试着更清楚地解释一下。
答案 2 :(得分:0)
试试这样......
的 actionbar.addTab(converterTab,0,假);
actionbar.addTab(favouriteTab,1,FALSE); 强>