2011-12-25 94 views
0

我正在從一個從互聯網上獲取信息的應用程序工作。信息按類別,子類別和子子類別分類。更改標籤主機意圖

我的主視圖是一個包含3個選項卡和初始列表視圖(子類別)的TabHost視圖(父類別)。當用戶點擊列表視圖中的項目時,它會調用一個新的列表視圖,顯示所選子類別的子類別。

我得到了一切工作,除了當選擇一個子類別時,tabHost視圖消失並且子子類別全屏顯示。

如何更改選項卡的意圖以顯示子類別的子類別?

編輯:這是我的代碼,對不起,我沒有發佈它!其中包含tabhost

我的主要觀點:

public class tabwidget extends TabActivity { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabs); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, category1Activity.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("category1").setIndicator("Category1", 
         res.getDrawable(R.drawable.ic_tab_category1)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent().setClass(this, category2Activity.class); 
    spec = tabHost.newTabSpec("category2").setIndicator("Category2", 
         res.getDrawable(R.drawable.ic_tab_category2)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, category3Activity.class); 
    spec = tabHost.newTabSpec("category3").setIndicator("Category3", 
         res.getDrawable(R.drawable.ic_tab_category3)) 
        .setContent(intent); 
    tabHost.addTab(spec); 


    tabHost.setCurrentTab(0); 
} 

當應用程序被啓動的酒標籤被默認選中。這是category1Acitivity列表視圖與調用子類別的onlclick行動:

listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
      //Toast.makeText(getApplicationContext(), "You clicked item at position"+position, 
      //Toast.LENGTH_SHORT).show(); 

      Toast.makeText(getApplicationContext(), "Loading "+((TextView) view.findViewById(R.id.categoryname)).getText(), 
      Toast.LENGTH_SHORT).show(); 

      Intent i = new Intent(category1Activity.this, subCategoryActivity.class); 
      i.putExtra("id", ((TextView) view.findViewById(R.id.message)).getText()); 
      i.putExtra("catname", ((TextView) view.findViewById(R.id.categoryname)).getText()); 
      i.putExtra("parentcatid", "0"); 
      startActivityForResult(i, ACTIVITY_CREATE); 




     } 
    }); 

的列表視圖,通過它發送到服務器的類ID生成是直接從數據庫結果。

回答

1

您將不得不使用ActivityGroups來做到這一點。

http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity

但是,請記住,活動組在ICS已被棄用。

編輯:這是我實現的ActivityGroup的:在標籤

活動:

Intent i = new Intent(v.getContext(), SearchList.class); 
i.putExtra("search", search); 

View view = SearchActivityGroup.group.getLocalActivityManager() 
.startActivity("SearchList", i 
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
.getDecorView(); 

// Again, replace the view 
SearchActivityGroup.group.replaceView(view); 

的ActivityGroup:

package nl.dante.SuperDeals; 

import java.util.ArrayList; 

import android.app.ActivityGroup; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

public class SearchActivityGroup extends ActivityGroup { 

View rootView; 

// Keep this in a static variable to make it accessible for all the nested 
// activities, lets them manipulate the view 
public static SearchActivityGroup group; 

// Need to keep track of the history if you want the back-button to work 
// properly, don't use this if your activities requires a lot of memory. 
private ArrayList<View> history; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    /* 
    * this.history = new ArrayList<View>(); group = this; 
    * 
    * // Start the root activity within the group and get its view View 
    * view = getLocalActivityManager().startActivity("Search", new 
    * Intent(this,Search.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
    * .getDecorView(); 
    * 
    * // Replace the view of this ActivityGroup replaceView(view); 
    */ 

} 

@Override 
protected void onResume() { 

    super.onResume(); 
    this.history = new ArrayList<View>(); 
    group = this; 

    // Start the root activity within the group and get its view 
    View view = getLocalActivityManager().startActivity("Search", new Intent(this, Search.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 

    // Replace the view of this ActivityGroup 
    replaceView(view); 
} 

public void replaceView(View v) { 
    // Adds the old one to history 
    if (history.size() == 0) { 
     if (rootView != null) { 
      history.add(rootView); 
      rootView = null; 
     } 
    } 
    history.add(v); 
    // Changes this Groups View to the new View. 
    setContentView(v); 
} 

public void back() { 
    try { 
     if (history.size() > 0) { 
      if (history.size() == 1) { 
       rootView = history.get(0); 
       Toasts.ToastImageView(this, "Druk nogmaals BACK om af te sluiten", R.drawable.power_64_off, "red"); 
      } 
      history.remove(history.size() - 1); 
      setContentView(history.get(history.size() - 1)); 
     } else { 
      finish(); 
     } 
     if (history.size() < 3) { 
      // Tabhost.bannerImage2.setImageResource(0); 
      Tabhost.banner.setBackgroundResource(R.drawable.gradient_blue); 
     } 
     if (history.size() == 2) { 
      Tabhost.bannerImage1.setImageResource(R.drawable.sorteer_btn); 
     } 
    } catch (Exception ex) { 
    } 
} 

public int getHistorySize() { 
    return history.size(); 
} 

@Override 
public void onBackPressed() { 
    try { 
     SearchActivityGroup.group.back(); 
    } catch (Exception ex) { 

    } 
    return; 
} 
} 
+0

太感謝你了!這是完美的,經過一些微調我設法讓它工作:) – 2011-12-27 20:30:03

+0

不客氣:) – Dante 2011-12-27 21:14:38