-1
我想获得一个示例代码来显示在我的主FragmentActivity中使用TabHost的新FragmentActivity。 FragmentActivity由五个选项卡组成,其中一个选项卡必须显示具有选项卡的新活动。 我的代码:FragmentActivity作为TabHost中的选项卡
TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
fm = getSupportFragmentManager();
venueFragment = (FragmentVenue) fm.findFragmentByTag("venue");
favoriteFragment = (FragmentFavorite) fm.findFragmentByTag("favorite");
venueTrendingFragment = (FragmentVenueTrending) fm.findFragmentByTag("venuetrendingnow");
searchFragment = (FragmentSearch) fm.findFragmentByTag("search");
accountFragment = (FragmentAccount) fm.findFragmentByTag("account");
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
/** Detaches the venueFragment if exists */
if(venueFragment!=null)
ft.detach(venueFragment);
/** Detaches the favoriteFragment if exists */
if(favoriteFragment!=null)
ft.detach(favoriteFragment);
/** Detaches the venueTrendingFragment if exists */
if(venueTrendingFragment!=null)
ft.detach(venueTrendingFragment);
/** Detaches the searchFragment if exists */
if(searchFragment!=null)
ft.detach(searchFragment);
if(accountFragment!=null)
ft.detach(accountFragment);
/** If current tab is venue */
if(tabId.equalsIgnoreCase("venue")){
if(venueFragment==null){
/** Create venueFragment and adding to fragment transaction */
ft.add(R.id.realtabcontent,new FragmentVenue(), "venue");
}else{
/** Bring to the front, if already exists in the fragment transaction */
ft.attach(venueFragment);
}
}else if(tabId.equalsIgnoreCase("favorite")){ /** If current tab is favorite */
if(favoriteFragment==null){
/** Create favoriteFragment and adding to fragment transaction */
ft.add(R.id.realtabcontent,new FragmentFavorite(), "favorite");
}else{
/** Bring to the front, if already exists in the fragment transaction */
ft.attach(favoriteFragment);
}
}else if(tabId.equalsIgnoreCase("venuetrendingnow")){ /** If current tab is venueTrendingFragment */
if(venueTrendingFragment==null){
/** Create venueTrendingFragment and adding to fragment transaction */
ft.add(R.id.realtabcontent,new FragmentVenueTrending(), "venuetrendingnow");
}else{
/** Bring to the front, if already exists in the fragment transaction */
ft.attach(venueTrendingFragment);
}
}else if(tabId.equalsIgnoreCase("search")){ /** If current tab is searchFragment */
if(searchFragment==null){
/** Create searchFragment and adding to fragment transaction */
//ft.add(R.id.realtabcontent,new FragmentSearch(MainActivity.this), "search");
//Intent intent = new Intent(this,SecondTabhost.class);
Intent intent = new Intent(MainActivity.this, SecondTabhost.class);
startActivity(intent);
}else{
/** Bring to the front, if already exists in the fragment transaction */
//ft.attach(searchFragment);
Intent intent = new Intent(MainActivity.this, SecondTabhost.class);
startActivity(intent);
}
}else if(tabId.equalsIgnoreCase("account")){ /** If current tab is accountFragment */
if(accountFragment==null){
/** Create accountFragment and adding to fragment transaction */
ft.add(R.id.realtabcontent,new FragmentAccount(), "account");
}else{
/** Bring to the front, if already exists in the fragment transaction */
ft.attach(accountFragment);
}
}
ft.commit();
}
};
里面不然,如果我用意图去下一个活动,因此延迟显示具有tabhost未来FragmentActivity上面的代码(tabId.equalsIgnoreCase(“搜索”))。我想毫不拖延地展示活动。
所以这里是前述问题? – 2013-03-20 10:09:17
我想获得添加新的FragmentActivity到选项卡的代码。 – 2013-03-20 10:11:02
【您应该阅读常见问题解答】(http://stackoverflow.com/faq),他们将帮助您更好地了解如何在此提出问题以及提出什么样的问题。 – adneal 2013-03-20 10:11:22