1. java 소스코드에 프래그먼트 추가하기

소스 코드에서 프래그먼트를 추가하기 위해선 프래그먼트 매니저를 사용해야 한다.

프래그먼트 매니저는 getSupportFragmentManager 메소드를 이용하여 참조한다. 



2. 실행 결과 화면

 



3. activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    android:padding="10dp"
    android:gravity="center">
 
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_marginTop="50dp"
       android:layout_marginBottom="30dp"
       android:gravity="center">
 
      <Button
          android:id="@+id/button1"
          android:layout_width="150dp"
          android:layout_height="wrap_content"
          android:text="프래그먼트1"/>
 
      <Button
          android:id="@+id/button2"
          android:layout_width="150dp"
          android:layout_height="wrap_content"
          android:text="프래그먼트2" />
   </LinearLayout>
 
   <FrameLayout
       android:id="@+id/frame_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
      <!--<fragment
          android:id="@fragment1main"
          android:nacom.tistory.qlyh8.pracitice.FragmentActivity11vity"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>-->
   </FrameLayout>
 
</LinearLayout>
 
cs



4. MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.tistory.qlyh8.pracitice;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity {
 
    Button button1, button2;
    FragmentActivity1 frameLayout1;
    FragmentActivity2 frameLayout2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        frameLayout1 = new FragmentActivity1();
        frameLayout2 = new FragmentActivity2();
 
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 프래그먼트 매니저가 프래그먼트를 담당하며, 프래그먼트 매니저은 트랜잭션에 기반한다.
                // replace(): 기존에 있는게 있으면 대체한다.
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.frame_layout, frameLayout1).commit();
            }
        });
 
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.frame_layout, frameLayout2).commit();
            }
        });
    }
}
cs



5. fragment1.xml 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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:background="#aaaaad"
    android:padding="10dp">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:textSize="30sp"
        android:textAlignment="center"/>
 
</LinearLayout>
 
cs



6.fragment2.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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:background="#aaddad"
    android:padding="10dp">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment 2"
        android:textSize="30sp"
        android:textAlignment="center"/>
 
</LinearLayout>
 
cs



7. FragmentActivity1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.tistory.qlyh8.pracitice;
 
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class FragmentActivity1 extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);
        return rootView;
    }
}
 
cs



8. FragmentActivity2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.tistory.qlyh8.pracitice;
 
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class FragmentActivity2 extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment2, container, false);
        return rootView;
    }
}
 
cs



참고

FragmentManager, FragmentTransaction 에 대해서:

http://www.kmshack.kr/2013/08/fragment-%ED%8C%8C%ED%97%A4%EC%B9%98%EA%B8%B0-3-fragmentmanager-fragmenttransaction%EC%97%90-%EB%8C%80%ED%95%B4%EC%84%9C/





출처: https://www.edwith.org/boostcourse-android/lecture/17074/ 


+ Recent posts