tabHost.setCurrentTab(...)を使用するとタブのコンテンツが変更されないという問題があります。対応するタブがアクティブとしてマークされていることがわかります。コンテンツは同じままです。タブをクリックすると、コンテンツが変更されます。Android TabHost - setCurrentTab()はタブの内容を変更しません。
セットアップに。
私はフラグメントを使用して1つのアプリで異なる機能を持っています。 1つのフラグメントにタブホストが含まれています。各タブには独自のレイアウトがあり、フラグメントはそのタブを切り替える必要があり、アプリケーションの状態に応じて選択したタブを表示する必要があります。
重要なコードの引用符を引用しようとします。
フラグメントのメイン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);
}
});
私はちょうどこの抜粋では2つのタブが含まれてきたが、これは基本的な手順になります。
すべてを再描画するために、タブホストを無効にしようとしました。これは何も変わらなかった。この問題の解決のためにここに上陸誰のための