2012-03-30 36 views
1

我有麻煩試圖找出如何將我當前的選項卡設置轉換爲一個使用視圖,而不是單獨的活動......我有調用我的搜索功能的問題,我認爲它是由於我創建我的標籤的方式。應用選項卡的意見,而不是單獨的活動

我主要的發射活動是public class Menu extends TabActivity它創建的標籤

intent = new Intent().setClass(this, TabGroup1.class); 
// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("codes").setIndicator("All Codes",res.getDrawable(R.drawable.ic_tab_codes)) 
.setContent(intent); 
tabHost.addTab(spec); 

`TabGroup1' 做的每個選項卡

public class TabGroup1 extends TabGroupActivity { 

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

然後調用ListActivity這下面顯示了有內容時的項目被點擊,另一個意圖被創建,然後開始一個新的活動,這使得我可以在用戶沿着列表進入時在每個級別上都有標籤。

這與下面的代碼

public void onListItemClick(ListView parent, View view, int position, long id) { 
    Intent intent = new Intent(getParent(), SubCategory.class); 
    Cursor cursor = (Cursor) adapter.getItem(position); 
    intent.putExtra("CATEGORY", cursor.getString(cursor.getColumnIndex("_id"))); 
    /*startActivity(intent);*/ 
    TabGroupActivity parentActivity = (TabGroupActivity)getParent(); 
    parentActivity.startChildActivity("SubCategory", intent); 
} 

TabGroupActivity做的是一類我從tutorial,可以讓你有相同的標籤佈局下的多個活動中。

我在努力的是將我必須使用的視圖轉換爲使用setContent來更改視圖。

我找到了this的例子,但它沒有提供足夠的細節讓我繼續。 還發現this一個,以及...

可有人請給我辦下來我需要什麼改變,也我怎麼使用我listactivities setContent ...

在此先感謝

+0

我很困惑...你每次改變標籤頁時都會創建新的標籤頁? – Barak 2012-03-30 22:29:42

+0

嗨@Barak我已經編輯了我的問題,上面有一些進一步的代碼,說明每個活動如何調用下一個代碼,以便選項卡位於頂端......它可以工作,但是我想從此轉換,因爲我已經讀取它不是是最好的方法,並且ActivityGroup已被棄用。這[教程](http://ericharlow.blogspot.ca/2010/09/experience-multiple-android-activities.html)是我用來創建我的標籤的人。 – DeucePie 2012-03-30 22:42:38

回答

0

幾件事... setContent定義了內容,它不會導致切換選項卡。如果要強制更改某個選項卡,請使用TabHost.setCurrentTab(tabid);,否則默認爲第一個選項卡,然後用戶選擇任何選項卡。

來自我自己的項目的一個例子(這個項目實際上有四個選項卡,但我已經刪除了一些嘗試並保持這一點)。有幾種方法可以做到這一點,但我發現最簡單的方法是創建一個方法來填充每個選項卡,這樣我可以根據需要刷新選項卡,方法是調用適當的方法來選擇我需要的選項卡(下面的所有Java都包含在內在TabActivity中)。

setupdetailmain.xml

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:padding="5dp" > 
      <TextView 
        android:id="@+id/setupheader" 
        android:layout_width="fill_parent" 
        android:layout_height="20dp" 
        android:textSize="15dp" /> 
      <TabWidget 
        android:id="@android:id/tabs" 
        android:layout_width="fill_parent" 
        android:layout_height="30dp" 
        android:gravity="bottom" /> 
      <FrameLayout 
        android:id="@android:id/tabcontent" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:padding="5dp" > 
      <!-- General Info Tab --> 
        <LinearLayout 
          android:id="@+id/note_tab" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:orientation="vertical" > 
        </LinearLayout> 
      <!-- Tool Tab --> 
        <LinearLayout 
          android:id="@+id/offset_tab" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:orientation="vertical" > 
          <ListView 
            android:id="@+id/list2" 
            android:layout_width="match_parent" 
            android:layout_height="0dp" 
            android:layout_weight="1" 
            android:drawSelectorOnTop="false" /> 
        </LinearLayout> 
      </FrameLayout> 
    </LinearLayout> 
</TabHost> 

標籤設置代碼(延伸TabActivity)

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.setupdetailmain); 

    // *************************************************************** 
    // Set up the tabs in the tabhost 
    // *************************************************************** 
    tabHost = getTabHost(); 
    TabHost.TabSpec spec; 
    spec = tabHost.newTabSpec("Offsets").setIndicator("Offsets") 
      .setContent(R.id.offset_tab); 
    tabHost.addTab(spec); 
    spec = tabHost.newTabSpec("Notes").setIndicator("Notes") 
      .setContent(R.id.note_tab); 
    tabHost.addTab(spec); 
    populateTabs(StartTab); 
} 

標籤填充方法

// *************************************************************** 
// Fill the Notes tab 
// *************************************************************** 
private void notestab() { 
    notes = (LinearLayout) findViewById(R.id.note_tab); 
    notestxt = new TextView(this); 
    notestxt.setText(SETUPINFO_NOTES); 
    notestxt.setTextSize(15); 
    notes.addView(notestxt); 
} 

// *************************************************************** 
// Fill the Offset tab 
// *************************************************************** 
private void offsettab() { 
    wpccount = 0; 
    for (int i = 0; i < 20; i++) { 
     if (wpcdesc[i] != null) { 
      wpcdisplayhold[wpccount] = wpcid[i] + " - " + wpcdesc[i]; 
      wpcidhold[wpccount] = wpcid[i]; 
      wpcdeschold[wpccount] = wpcdesc[i]; 
      wpccount++; 
     } 
    } 
    wpcdisplay = new String[wpccount]; 
    for (int i = 0; i < wpccount; i++) { 
     wpcdisplay[i] = wpcdisplayhold[i]; 
    } 
    mWPCView = (ListView) findViewById(R.id.list2); 
    mWPCView.setAdapter(new ColorizingListAdapter(SetupDisplay.this, 
      wpcdisplay, "Offset")); 
    registerForContextMenu(mWPCView); 
} 

這使用了一個自定義適配器,但希望它能夠實現如何在選項卡視圖中設置列表而不必執行新活動的想法。

相關問題