0
0

More than 3 years have passed since last update.

[ Android ] Fragment基礎2

Posted at

前回記事Fragment基礎 https://qiita.com/QiitaD/items/edb3d99ce03cd000b23f
に続き、今回もFragmentについて触れていく。

前回の補足

前回触れた使いまわしについてよくわからなかったので、調べてみた結果を綴る。
まず例として画面の
左側にボタン群、右側に入力欄群があるプログラムを考える。

スクリーンショット (80).png

例えば、これをFragmentで分けると以下になる。

スクリーンショット (82).png

上記をファイル名に置き換えると以下のようにできる。
スクリーンショット (83).png

つまり、
activity_main.xmlでボタンや入力欄を定義するのではなく、
2つのFragmentのレイアウトを独立して定義し、
activity_main.xmlで2つのFragmentの位置を決めるということである。

それ故、仮に新しいレイアウトactivity_sub.xmlで上記と同じFragmentを使いたい場合、
activity_sub.xmlに使いたいレイアウトを持つFragmentを定義すればよい
(=使いまわしができる)ということである。

実装

上記のイメージ図を実装してみる。

Fragment実装に必要なファイルは
レイアウトファイ(xml)とそのロジックを書くファイル(java)である。
全体で必要となったのは以下の6つである。
1. MainActivity.java
2. activity_main.xml
3. Fragment01.java(ボタン二つだけにしてある)
4. fragment01.xml
5. Fragment02.java
6. fragment02.xml

(ただし、今回はFragment生成を確認すればよいだけなので、
クリックイベントなどは一切実装しない。)

サンプルは長くなるので「まとめ」の後に載せる。

結果

以下の結果が得られた。
成功である。
スクリーンショット (85).png

まとめ

以前画面遷移について記事を書いたことがある。
https://qiita.com/QiitaD/items/1495f2f9839fe8706d31
この記事ではIntentを使って画面遷移を行ったが、
Fragmentでも画面遷移をすることができる。
記事では触れないが、ぜひ試してみて頂きたい。

サンプル

  1. MainActivity.java
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
  1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.fragmentsample02.Fragment01"
        android:layout_width="150dp"
        android:layout_height="300dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="116dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragmentsample02.Fragment02"
        android:layout_width="150dp"
        android:layout_height="300dp"
        android:layout_marginStart="200dp"
        android:layout_marginTop="116dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. Fragment01.java
public class Fragment01 extends Fragment {

    @Override
    public View onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment01, container, false);
    }
}
  1. fragment01.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Fragment01">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="52dp"
        tools:layout_editor_absoluteY="49dp">

        <Button
            android:id="@+id/buttonInputName"
            android:layout_width="150dp"
            android:layout_height="105dp"
            android:text="Input Name" />

        <Button
            android:id="@+id/buttonInputAge"
            android:layout_width="150dp"
            android:layout_height="105dp"
            android:layout_marginStart="0dp"
            android:layout_marginTop="114dp"
            android:text="Input Age" />
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
  1. Fragment02.java
public class Fragment02 extends Fragment {

    @Override
    public View onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment02, container, false);
    }
}

  1. fragment02.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Fragment01">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="52dp"
        tools:layout_editor_absoluteY="49dp">

        <EditText
            android:id="@+id/buttonInputName"
            android:layout_width="390dp"
            android:layout_height="105dp"
            android:text="input->" />

        <EditText
            android:id="@+id/buttonInputAge"
            android:layout_width="390dp"
            android:layout_height="105dp"
            android:layout_marginStart="0dp"
            android:layout_marginTop="114dp"
            android:text="confirm->" />
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0