android-fragments チュートリアル
android-fragmentsを使い始める
サーチ…
備考
フラグメントは、アンドロイドアプリのユーザーインターフェイスの非常に重要なコンポーネントです。 Android 3.0(Honeycomb)APIで最初に導入されました。
フラグメントの設計パラダイムを理解する
フラグメントは、主にタブレットなどの大画面デバイス上のモジュラーで柔軟なUIをサポートするために導入されました。
フラグメントはアクティビティによって管理されます。通常、各フラグメントは画面の一部を表します。 1つのアクティビティに複数のフラグメントが存在する可能性があります。フラグメントはサブアクティビティと呼ばれることもあります。フラグメントをアクティビティレイアウトの一部として追加すると、フラグメントはアクティビティのビュー階層内のViewGroup内に存在し、フラグメントは独自のビューレイアウトを定義します。
ライフサイクル
ちょうどアクティビティと同様に、フラグメントもライフサイクルを持っています。フラグメントは、次のイベントについて通知されます。
- アクティビティに接続する - onAttach(アクティビティ)
- フラグメントの作成 - onCreate(バンドル)
- ビューの作成 - onCreateView(LayoutInflater、ViewGroup、Bundle)
- アクティビティの作成 - onActivityCreated(バンドル)
- ビューの状態が復元されました - onViewStateRestored(バンドル)
- ユーザーに表示されるようになりました - onStart()
- ユーザインタラクションの開始 - onResume()
- ユーザインタラクションの一時停止 - onPause()
- ユーザーに見えないようにしました - onStop()
- ビューの破壊 - onDestroyView()
- フラグメントを破棄する - onDestroy()
- アクティビティから分離する - onDetach()
プログラマは、さまざまなライフサイクルコールバックメソッドをオーバーライドする必要があります。通常、onCreate()、onCreateView()、onPause()メソッドを実装します。
フラグメントのサブクラス
- DialogFragment - フローティングダイアログを表示する
- ListFragment - アイテムのリストを表示する
- PreferenceFragment - 設定アクティビティの作成に役立ちます
参考文献
フラグメントを追加する
フラグメントを静的に追加する
ファイル: activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentexample.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentexample.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
ファイル: fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ff00"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment frist"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
ファイル: fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0000ff"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Fragment"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
ファイル: MainActivity.java
package com.example.fragmentexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ファイル: Fragment1.java
package com.example.fragmentexample;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment1,container, false);
}
}
ファイル: Fragment2.java
package com.example.fragmentexample;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment2,container, false);
}
}
フラグメントを動的に追加する
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/container1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<FrameLayout
android:id="@+id/container2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
FrameLayoutはフラグメントコンテナとして動作します。
MainActivityクラス
ファイル:MainActivity.java
package com.example.fragmentexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragment(this, R.id.container1,new Fragment1(),"fragment1");
loadFragment(this, R.id.container2,new Fragment2(),"fragment2");
}
public static void loadFragment(Activity activity, int containerId, Fragment fragment, String tag)
{
activity.getSupportFragmentManager().beginTransaction().
replace(containerId, fragment,tag).commitAllowingStateLoss();
}
}