LoginSignup
11
10

More than 5 years have passed since last update.

FragmentTabHost覚書

Last updated at Posted at 2015-10-28

FragmentTabHostの簡単なサンプルです

support.v4.app.FragmentManager
support.v4.app.FragmentTabHost
を使っています。

サンプルプログラム

MainActivity.java
public class MainActivity extends AppCompatActivity{

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

        //FragmentManagerの取得
        FragmentManager mFragmentManager = getSupportFragmentManager();
        //xmlからFragmentTabHostを取得、idが android.R.id.tabhost である点に注意
        FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        //ContextとFragmentManagerと、FragmentがあたるViewのidを渡してセットアップ
        tabHost.setup(this, mFragmentManager, R.id.content);

        //String型の引数には任意のidを渡す
        //今回は2つのFragmentをFragmentTabHostから切り替えるため、2つのTabSpecを用意する
        TabSpec mTabSpec1 = tabHost.newTabSpec("tab1");
        TabSpec mTabSpec2 = tabHost.newTabSpec("tab2");

        //Tab上に表示する文字を渡す
        mTabSpec1.setIndicator("This is tab1");
        mTabSpec2.setIndicator("This is tab2");

        Bundle args = new Bundle();
        args.putString("string", "message");

        //それぞれのTabSpecにclassを対応付けるように引数を渡す
        //第3引数はBundleを持たせることで、Fragmentに値を渡せる。不要である場合はnullを渡す
        tabHost.addTab(mTabSpec1, Fragment1.class, args);
        tabHost.addTab(mTabSpec2, Fragment2.class, null);
    }
}
Fragment1.java
public class Fragment1 extends Fragment {

   @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

       //addTabの際にBundleを渡す場合は、Bundleから値を取得する
       //渡さない(nullを渡している)場合は、実装しなくてよい。そうでないとgetString("string")時にエラーが発生する
        Bundle args = getArguments();
        String str = args.getString("string");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_tab_host_fragment_1, null);
        return v;
    }
}
Fragment2.java
public class Fragment2 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_tab_host_fragment_2, null);
        return v;
    }
}

2箇所について、idが指定されている点に注意

fragment_tab_host_activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<!-- FragmentTabHostのidは必ず @android:id/tabhost にする-->
<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- FragmentTabHost同様、idの指定あり。idは必ず @android:id/tabs にする-->
        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <!-- contentにFragmentが追加される-->
        <FrameLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

fragment_tab_host_fragment1.xml、~2.xmlともにTextView一つのlayout

fragment_tab_host_fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="fragment1"
        android:id="@+id/textView1"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
fragment_tab_host_fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="fragment2"
        android:id="@+id/textView2"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

参考:
http://dev.classmethod.jp/smartphone/android/android-tips-38-fragment-tab-host/

ありがとうございました。

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10