新人成長記録4th
こんにちは、Android開発プログラマのpoppyです
前回の記事では、
ListViewにおける動的なViewの生成をご紹介いたしました
今回は、Fragmentについて記事にしていきたいと思います
ではでは、以下の内容でさっそくいきますよ~
【Activityのレイアウト】
【実装編(Fragmentクラスの作成)】
【実装編(Activityからの呼び出し)】
【終わりに】
Activityのレイアウト
ささっとMainActivityのレイアウトを紹介します
TextViewでタブっぽいものを3つ用意します
タップされたらFrameLayoutの中が変化するようにしていきます
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="タブ1"
android:onClick="onClick"/>
<TextView android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="タブ2"
android:onClick="onClick"/>
<TextView android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="タブ3"
android:onClick="onClick"/>
</LinearLayout>
<FrameLayout android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
実装編(Fragmentクラスの作成)
ここからはプログラミングのお時間です
Fragmentクラスの実装方法を紹介していきます
Fragmentを継承したクラスを作成します
このクラスで目的のレイアウトを生成します
Inflaterを使用してxmlからレイアウトを取得したり、
インスタンスを生成して戻り値とすることで、動的に生成したり自由にできます
public class myFragment extends Fragment{
//コンストラクタ、特になにもしない
public myFragment(){}
//commit時に呼ばれる(Viewが描画される時)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle bundle){
//inflate(int resource, ViewGroup root, boolean attachToRoot)
//inflate(挿入したいレイアウト,親となるViewGroup, Viewのrootを取るかどうか?)
View v= inflater.inflate(R.layout.contentlayout,container,false);
//取得したViewを返す
return v;
}
}
例では、Viewを取得してからすぐに終了しました
Viewに手を加えないようなら
return inflater.inflate(…);としても大丈夫。むしろこちらなら1行で記述できる
でも、手を加えないならそもそもFragmentで呼ぶ必要はあるのかどうか
短いならActivity内で記述した方がいい気もする
手を加えるなら(xmlのレイアウトを使用)
型 変数名 = (型)v.findViewById();で
インスタンスの生成ができるので、内容を編集していこう
手を加えるなら(動的に生成)
型 変数名 = new 型
で生成したいオブジェクトのインスタンスを取得していく
最後に戻りにしたら問題なし
inflateメソッドは引数が2つのinflate(int resource, ViewGroup root)も存在する
それぞれの引数に変化を与えて実行した結果、得られるViewの値は以下のようになった
引数が2つ | 引数が3つ(true) | 引数が3つ(false) | |
---|---|---|---|
rootが有 | rootに指定したView | sourceに指定したView | rootに指定したView |
rootがnull | sourceに指定したView | sourceに指定したView | sourceに指定したView |
適切な場面での使い分けはまだ理解できていないですが、
今回はsourceであるViewを取得するようにしましょう
実装編(Activityからの呼び出し)
Activityの紹介をしていきましょう
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//初回表示のFragmentを表示
setFragment(new topFragment());
setContentView(R.layout.activity_main);
}
//各テキストをクリック時のイベント
public void onClick(View v){
switch (v.getId()){
case R.id.tab1:
setFragment(new myFragment());
break;
case R.id.tab2:
setFragment(new my2Fragment());
break;
}
}
//Fragment表示のメソッド
private void setFragment(Fragment fragment){
//レイアウトの切り替えを行う
FragmentTransaction ft = getFragmentManager().beginTransaction();
//どのレイアウトに追加するか、すでにFragmentが存在すれば削除してから追加する
ft.replace(R.id.fragment_frame, fragment);
//Fragmentの遷移の仕方
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//実際に表示
ft.commit();
}
}
このように実装しました。
Fragmentはcommitをしないと表示されないので注意が必要です
終わりに
前回のListViewを動的に生成するよりは理解しやすかったです
ListViewとFragment、どちらもよく使うと思いますので、しっかりと覚えておきたいです
最近は、IntentやServiceに触れる機会がありましたが、正直難しいです
早いうちに勉強するので、近いうちに記事にしたいと考えています
また、Object指向への理解も低いのが見受けられる所が多々ありました
そのため、Object指向のことをしっかりと勉強する必要がありそうです
記事のネタが豊富なうちはまだまだ未熟と思いますので、必死に勉強するのみです!
以上、火曜日担当:poppyからでした
admin at 2016年07月19日 10:00:01