Androidで標準APIのFragmentを使用し、Fragmentを動的に変更します。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidSample53-002-FragmentChange」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.androidsample53_002 |
| プロジェクト名(Project Name) | AndroidSample53-002-FragmentChange |
| パッケージ名(Package Name) | com.example.androidsample53_002 |
| Minimum Required SDK | API 14 |
| Target SDK | API 14 |
| Compile With | API 14 |
| Theme | Theme Holo Light |
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidsample53_002"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="AndroidSample53-002"
android:theme="@style/AppTheme" >
<activity android:name="com.example.androidsample53_002.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「res/values/styles.xml」は以下の通り。
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light" ></style>
</resources>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順4】
1.「res/layout/activity_main.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickChange"
android:text="変更" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「任意のファイルの作成方法」の手順で「res/layout/fragment1.xml」を作成。
2.「res/layout/fragment1.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello"
android:textColor="@android:color/black" />
</LinearLayout>
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。4.「Ctrl+S」を押し、ファイルを保存。
【手順6】
1.「任意のファイルの作成方法」の手順で「res/layout/fragment2.xml」を作成。
2.「res/layout/fragment2.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="World"
android:textColor="@android:color/white" />
</LinearLayout>
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。4.「Ctrl+S」を押し、ファイルを保存。
【手順7】
1.「src/com/example/androidsample53_002/MainActivity.java」は以下の通り。
package com.example.androidsample53_002;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import com.example.androidsample53_001.R;
public class MainActivity extends Activity {
private boolean fragment1On = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeFragment();
}
public void onClickChange(View view) {
changeFragment();
}
private void changeFragment() {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (fragment1On) {
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.fragment_container, fragment2);
} else {
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.fragment_container, fragment1);
}
fragment1On = !fragment1On;
fragmentTransaction.commit();
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順8】
1.「src/com/example/androidsample53_002/Fragment1.java」は以下の通り。
package com.example.androidsample53_002;
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) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順9】
1.「src/com/example/androidsample53_002/Fragment2.java」は以下の通り。
package com.example.androidsample53_002;
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 {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順10】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順11】
1.以下の様に表示されれば成功です。
以上です。


0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。