-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