Androidはワンツーパンチ 三歩進んで二歩下がる

プログラミングやどうでもいい話

Hello WorldをFragmentを使って書き換えた

自分の勉強メモです。

このエントリーにはその2があります。

下準備
FragmentはHoneycombで追加された機能だけどHoneycombエミュレータがどうもうまく動かないので、Compatibility packageを使って、Fragmentを低いターゲット(2.2としておきます)で動かすこととする。
それには android-support-v4.jar というライブラリーをプロジェクトに追加する必要がある。
こちらのサイト様が参考になります。
または、

プロジェクトを右クリック→
Android Tools→
Add Compatibility Library

を行うと、Referenced Librariesの中にandroid-support-v4.jarが追加される。

今までのHello Worldのソース

res/layout/main.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout> 

MainActivity.java

package com.sakurafish.android.fragmenttest.helloworld;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Fragmentを使って書き換えたHello Worldのソース

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Fragment クラスを定義 -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:name="com.sakurafish.android.fragmenttest.helloworld.FragmentTest" />

res/layout/fragment1.xml(今までのmain.xmlと同じ)

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

MainActivity.java

package com.sakurafish.android.fragmenttest.helloworld;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    // Compatibility Package を使う場合はActivityではなくFragmentActivityを継承する
    // こうしないと例外が発生する
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Fragmentを継承したクラスを作成する
FragmentTest.java

package com.sakurafish.android.fragmenttest.helloworld;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTest extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment1, container);

        /** 戻り値としてインフレートした View を返す */
        return root;
    }
}

挙動は今までのHello Worldと全く同じ。