我以前做过这样的事情,这是一个非常简单的例子,但我似乎无法使用actionbarsherlock v4.1。
这是主要活动
public class VanityActivity extends SherlockFragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
//restore
}
com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
com.actionbarsherlock.app.ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Website");
tab1.setTabListener(new HeaderTabListener(getApplicationContext()));
tab1.setTag(1);
actionBar.addTab(tab1);
com.actionbarsherlock.app.ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Portfolio");
tab2.setTabListener(new HeaderTabListener(getApplicationContext()));
tab2.setTag(2);
actionBar.addTab(tab2);
com.actionbarsherlock.app.ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Team");
tab3.setTabListener(new HeaderTabListener(getApplicationContext()));
tab3.setTag(3);
actionBar.addTab(tab3);
}
}
我的HeaderTabListener类:
public class HeaderTabListener implements
com.actionbarsherlock.app.ActionBar.TabListener {
private Context context;
public HeaderTabListener(Context context){
this.context = context;
}
@Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
int i=((Integer)tab.getTag()).intValue();
if(tab.getPosition()==0){
ft.replace(android.R.id.content, CompanyFragment.newInstance(i));
}else if(tab.getPosition()==1){
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i));
}
}
@Override
public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
Log.v("Tab selected", tab.getText().toString());
}
@Override
public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
Log.v("Tab selected2", tab.getText().toString());
}
}
最后一个片段(两个片段相同但每个布局中的文本不同)
public class PortfolioFragment extends SherlockFragment {
private static final String KEY_POSITION="position";
private static final String KEY_TEXT="text";
static PortfolioFragment newInstance(int position) {
PortfolioFragment frag=new PortfolioFragment();
Bundle args=new Bundle();
args.putInt(KEY_POSITION, position);
frag.setArguments(args);
return(frag);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_portfolio, container, false);
int position=getArguments().getInt(KEY_POSITION, -1);
return(result);
}
}
因此,当它首次加载时,选项卡全部正确显示,突出显示的第一个选项卡和CompanyFragment布局中的文本显示。但是后来我无法点击任何其他选项卡来更改内容,而onTabSelected中的日志永远不会被调用。有什么想法吗?谢谢你的阅读。
答案 0 :(得分:0)
首先,让我告诉你我没有使用ActionBarSherlock的经验。我看了你的代码,为我的应用程序提供了一些想法。看到自从你几个月前发布以来它没有得到解答,我想分享我的第一印象。
据我所知,ActionBarSherlock公开了与最新Android API相同的功能。因此,我认为你缺少一个简单的方法调用。更换片段后,您需要调用commit()
(Android Developer Guide reference)。因此,更改行和所有类似的行;
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i));
至
ft.replace(android.R.id.content, PortfolioFragment.newInstance(i)).commit();
。
我希望这可以解决你的问题,虽然我确实意识到你声明永远不会调用ònTabSelected()`方法。但是,如果没有调试,问题之间的差异就会显得透明。