1

我有使用tabHost.setCurrentTab(...)時標籤內容不會更改的問題。可以看到,相應的選項卡被標記爲活動,內容保持不變。當我點擊標籤時,內容會改變。Android TabHost - setCurrentTab()不會更改標籤內容

到設置。

我在一個使用片段的應用程序中有不同的功能。一個片段包含一個tabhost。每個選項卡都有自己的佈局,片段應該在這些選項卡之間切換,並根據應用程序的狀態顯示選項卡選項。

我會盡量引用重要的代碼段落。

片段的主要xml文件:包括

<TabHost 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/tabHost" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:background="@color/colorPrimary"/> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      > 

      <include layout="@layout/fragment_master_emg_start" 
       android:layout_height="445dp" 
       android:layout_width="fill_parent" 
       android:layout_gravity="center|bottom"/> 

      <include layout="@layout/fragment_master_emg_setup" 
       android:layout_height="445dp" 
       android:layout_width="fill_parent" 
       android:layout_gravity="center|bottom"/> 

    ... some more includes 

    </FrameLayout> 
</TabHost> 

XML文件中只包含一個相對佈局和一堆按鈕和textviews的。

碎片onCreate方法:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Get View 
    View rootView = inflater.inflate(R.layout.fragment_master_emg, container, false); 
    Button button= (Button)rootView.findViewById(R.id.button); 
    button.setText("test"); 
    TabHost tabHost=(TabHost)rootView.findViewById(R.id.tabHost); 
    tabHost.setup(); 
    // Tabs 
    // Setup - Start Tab 

    TabHost.TabSpec t=tabHost.newTabSpec("Tab0"); 
    t.setContent(R.id.master_emg_start); 
    t.setIndicator("EMG Measurement - Setup"); 
    tabHost.addTab(t); 

    TabHost.TabSpec t0=tabHost.newTabSpec("Tab1"); 
    t0.setContent(R.id.master_emg_setup); 
    t0.setIndicator("Setup"); 
    tabHost.addTab(t0); 

    .... more tabs 
    .... 

    // Set initial visibility 
    tabHost.getTabWidget().getChildAt(0).setVisibility(View.VISIBLE);   
    tabHost.getTabWidget().getChildAt(1).setVisibility(View.GONE); 

    // Buttons to switch tabs 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      TabHost tabHost=(TabHost)getActivity().findViewById(R.id.tabHost); 
      tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE); 
      tabHost.getTabWidget().getChildAt(1).setVisibility(View.VISIBLE); 
      tabHost.getTabWidget().setCurrentTab(1); 
     } 
    }); 

我只是包含在此摘錄兩個標籤,但這將是基本的程序。

我試圖失效標籤主機,以便重新繪製所有的東西。這沒有改變任何東西。

回答

0

對於任何人登陸這裏對於這個問題的解決方案:

用途:

tabHost.setCurrentTab(tabNumber); 

相反的:

tabHost.getTabWidget().setCurrentTab(tabNumber);