Android 初心者がフラグメントを使う話
Android アプリ開発の入門書を手に取ると、まず画面の作成に始まり、必要な部品を追加してメソッドを実装し、他の画面への遷移へと順に進む方針で書かれていることが一般的です。
そこでは基本的にアクティビティ (Activity) というコンポーネントを使用することを前提に話が進みます。
- アクティビティ | Android Developers
- https://developer.android.com/guide/components/activities.html?hl=ja
- フラグメント | Android Developers
- https://developer.android.com/guide/components/fragments.html?hl=ja
public class FooFragment extends Fragment { /* 省略 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_foo, container, false); // view.findViewById() などで何かする場合にはここに実装 return view; } } 1点目はコード中の R.layout.fragment_foo で参照されている XML ファイル fragment_foo.xml を作成してあげること。
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.femoghalvfems.sample.FooFragment">
<!-- 省略 -->
</android.support.constraint.ConstraintLayout>
もう1点目は作成したフラグメントをアクティビティに追加してあげることです。
public class MainActivity extends FragmentActivity
implements FooFragment.OnFragmentInteractionListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FooFragment fragment = new FooFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.add(R.id.layout_activity_main, fragment);
transaction.commit();
}
}
この時、追加するフラグメントの OnFragmentInteractionListener を実装してあげないと InflateException を吐いてアプリが落ちますので、忘れずに実装してください。
InflateException は他のエラーと同様に Android Monitor の logcat から確認できます。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: info.femoghalvfems.sample, PID: 13994
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.femoghlavfems.sample/info.femoghalvfems.sample.MainActivity}: android.view.InflateException: Binary XML file line #12:
Binary XML file line #12: Error inflating class fragment
### 省略 ###
Caused by: java.lang.RuntimeException:
info.femoghlavfems.sample.MainActivity@87ac4dd must implement OnFragmentInteractionListener
これでうまくビルドに成功すれば、アプリの立ち上げ画面からフラグメントのレイアウトを表示するようになります。
レイアウトを切り替えるにはフラグメントから FragmentManager を利用します。
public class FooFragment extends Fragment implements View.OnClickListener {
/* 省略 */
@Override
public void onClick(View v) {
FragmentManager manager = getActivity().getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout_activity_main,
BarFragment.newInstance("", "")).addToBackStack(null).commit();
}
}
この時に忘れてはならないのは、追加されるアクティビティ (ここでは MainActivity) に追加するフラグメント (ここでは BarFragment) の OnFragmentInteractionListener を追加実装してあげることです。
このようにアクティビティと比較するとやや煩雑になってしまいますが、入門書で作成したサンプルアプリを使ってアクティビティからフラグメントを使用した構成に自分でコードを書き換えていくと、アプリ開発についての理解が深まって良い勉強になります。
そしてあまり入門書では触れられていない、FragmentTabHost を用いたタブの切り替えも簡単に行えるようになる点も見逃せません。
コメント
まだコメントはありません。最初のコメントをどうぞ。
コメントを残す