2012-07-31 77 views
-1

這裏更多的意圖是我的問題: 我有一個正常的tabactivity,與我的三個標籤定義像這樣的:安卓:tabhost比標籤

TabSpec listatabs= tabHost.newTabSpec("App"); 
    listatabs.setIndicator("Lista", getResources().getDrawable(R.drawable.mia_lista)); 
    Intent listaIntent = new Intent(this, MIALISTA.class); 
    listaIntent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    listatabs.setContent(listaIntent); 
    // Aggiungo i tabs 
    tabHost.addTab(listatabs); 

的問題是:如果用戶在這個選項卡中去(其中有一個項目列表),並點擊一個項目,我必須調用一個新的意圖來顯示內容(當然還有一個新的.class)...但我必須留在intrested選項卡中。 我該如何實現這個想法?

+0

這些項目,你說的是標籤嗎?或者他們是否希望在當前選項卡下看到其他活動? – slybloty 2012-07-31 14:40:45

+0

其他活動...我認爲這是一個這樣的想法,但我不知道,因爲這是一個新的東西對我來說:http://stackoverflow.com/questions/1306689/launching-activities-within-a-tab-在Android – Zak 2012-07-31 14:41:23

回答

2

使用自定義的ActivityGroup:

使類TabGroupActivity:

public class TabGroupActivity extends ActivityGroup { 

private ArrayList<String> mIdList; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (mIdList == null) mIdList = new ArrayList<String>(); 
} 

/** 
* This is called when a child activity of this one calls its finish method. 
* This implementation calls {@link LocalActivityManager#destroyActivity} on the child activity 
* and starts the previous activity. 
* If the last child activity just called finish(),this activity (the parent), 
* calls finish to finish the entire group. 
*/ 
@Override 
public void finishFromChild(Activity child) { 
    LocalActivityManager manager = getLocalActivityManager(); 
    int index = mIdList.size()-1; 

    if (index < 1) { 
     finish(); 
     return; 
    } 

    manager.destroyActivity(mIdList.get(index), true); 
    mIdList.remove(index); 
    index--; 
    String lastId = mIdList.get(index); 
    Intent lastIntent = manager.getActivity(lastId).getIntent(); 
    Window newWindow = manager.startActivity(lastId, lastIntent); 
    setContentView(newWindow.getDecorView()); 
} 

/** 
* Starts an Activity as a child Activity to this. 
* @param Id Unique identifier of the activity to be started. 
* @param intent The Intent describing the activity to be started. 
* @throws android.content.ActivityNotFoundException. 
*/ 
public void startChildActivity(String Id, Intent intent) { 
    Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
    if (window != null) { 
     mIdList.add(Id); 
     setContentView(window.getDecorView()); 
    } 
} 

/** 
* The primary purpose is to prevent systems before android.os.Build.VERSION_CODES.ECLAIR 
* from calling their default KeyEvent.KEYCODE_BACK during onKeyDown. 
*/ 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
if (keyCode == KeyEvent.KEYCODE_BACK) { 
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR 
return true; 
} 
return super.onKeyDown(keyCode, event); 
} 

/** 
* Overrides the default implementation for KeyEvent.KEYCODE_BACK 
* so that all systems call onBackPressed(). 
*/ 
@Override 
public boolean onKeyUp(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     onBackPressed(); 
     return true; 
    } 
    return super.onKeyUp(keyCode, event); 
} 

/** 
* If a Child Activity handles KeyEvent.KEYCODE_BACK. 
* Simply override and add this method. 
*/ 
@Override 
public void onBackPressed() { 
    int length = mIdList.size(); 
    if (length > 1) { 
     Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1)); 
     current.finish(); 
    } 
} 
在您的自定義活動

,例如 'HomeTabGroupActivity':

public class HomeTabGroupActivity extends TabGroupActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    startChildActivity("HomeMainActivity", new Intent(this, HomeMainActivity.class)); 

} 
} 
在活動A

,用這個來去activityB:

Intent i = new Intent(getParent(),ActivityB.class); 
     TabGroupActivity parentActivity = (TabGroupActivity)getParent(); 
     parentActivity.startChildActivity("ActivityB", i); 

讓標籤帶你到'HomeTabGroupActivity'

讓我知道如果你想使用tabgroup中的startActivityForResult,虐待發布一點點的修復。

+0

我試圖實現這一點,我會讓你知道 – Zak 2012-07-31 14:57:13

+0

好的想法是這樣的。現在感謝! – Zak 2012-07-31 15:09:20