Android用のダメージ計算機をつくる 回想記3

2つ以上の画面を使う方法として、最初に見つけたのがIntentを使う方法。これで別のActivity(画面)を呼び出すことで、画面の切り替えができる。
これに乗っ取って別画面もActivityで構築、画面遷移ボタンを実装して挙動も確認。
ここまではよかったのだが、この方法だと各画面に画面遷移ボタンを画面の数だけ配置しなければならない。2画面程度ならともかく、3画面、4画面と増やしていくと面倒。


次に見つけた方法は、FragmentTabHostを使う方法。
名前の通りタブ形式で切り替えることができる。そして、名前の通りFragmentを使う。
切り替え先として登録できるものはFragmentだけで、この時点で作っていたActivityを全てFragmentに書き換えることに……
書き換えがわりと簡単にできたのは幸い。


FragmentTabHostを定義するレイアウトでスクロールできるように(ScrollViewを)仕込んでおくと、すべてのタブでスクロールできるようになったのは想定外の収穫。

<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">
 
		<TabWidget
			android:id="@android:id/tabs"
			android:orientation="horizontal"
			android:layout_width="match_parent"
			android:layout_height="48dp"
			android:layout_weight="0"/>
 
		<FrameLayout
			android:id="@android:id/tabcontent"
			android:layout_width="0dp"
			android:layout_height="0dp"
			android:layout_weight="0"/>
		
		<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
				 
			<FrameLayout
				android:id="@+id/content"
				android:layout_width="match_parent"
				android:layout_height="0dp"/>
				
		</ScrollView>
		
	</LinearLayout>
	
</android.support.v4.app.FragmentTabHost>

参考にしたサイト
TechBooster - Intentで画面遷移する(明示的Intent)/Getting started
Android Tips #38 FragmentTabHost を使って Fragment をタブで切り替える
Android Code Blogspot - Android Fragment Tab Example | Bottom Fragment Tab | Custom Tab Item