안드로이드 프레그먼트(Fragment) 예제


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
 
public class MainActivity extends AppCompatActivity {
 
    Fragment1 fragment1;
    Fragment2 fragment2;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //fragment 객체
        fragment1 = (Fragment1)getSupportFragmentManager().findFragmentById(R.id.mainFragment);
        fragment2 = new Fragment2();
    }
 
    //fragment 교체
    public void onFragmentChanged(int index){
        if(index==0){
            //fragment manager 를 이용하여 현재의 fragment를 교체
            //begin transaction를 이용하여 롤백할 수 있도록한다
            //commit를 호출하여 실행
            getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment2).commit();
        }else if(index==1){
            getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment1).commit();
        }
    }
}
cs



activity_main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#eee"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <!-- fragment 는 RelativeLayout 안에 있어야 한다 -->
    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/mainFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.ghj.ex_01.Fragment1"/>
    </RelativeLayout>
 
</LinearLayout>
 
cs



Fragment1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Fragment1 extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //화면 inflate
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment1, container, false);
 
        //버튼 이벤트정의
        Button button = (Button)rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //fragment 를 호출한 activity 얻기
                MainActivity activity = (MainActivity)getActivity();
                activity.onFragmentChanged(0);
            }
        });
        return rootView;
    }
}
cs



fragment1.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#f00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:text="첫번째 프래그먼트"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Button
        android:id="@+id/button"
        android:text="두번째 화면으로"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
cs



Fragment2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Fragment2 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);
 
        Button button = (Button)rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                MainActivity activity = (MainActivity)getActivity();
                activity.onFragmentChanged(1);
            }
        });
        return rootView;
    }
}
cs



fragment2.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#00f"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:text="두번째 프래그먼트"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Button
        android:id="@+id/button"
        android:text="첫번째 화면으로"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
cs



결과

 


+ Recent posts