Android

Android] Fragment 란 무엇인가? (Fragment 예제)

김코식 2022. 5. 30. 20:04

Fragment : 하나의 Activity안에 독립적으로 동작하는 UI의 일부분

 

Fragment 특징 

  • 자체 레이아웃과 자체 생명주기를 가지기 때문에 Activity 화면 안에 부분 화면을 독립적으로 사용할 수 있다.
  • 자체 입력 이벤트를 받고 처리할 수 있다.
  • 독립적으로 존재할 수 없고 Activity에 종속되어야 한다.

Fragment 장점

  • 모듈화를 통한 독립적인 UI로 구현이 가능하여 관리가 용이하다.
  • 여러 Activity에서 재사용이 가능하다.
  • UI screen 없이 동작이 가능하고 Activity에서 보이지 않게 동작이 가능하다.

Fragment 단점

  • Activity가 특정 Fragment와 연관성이 강해짐
  • Layout에 고려할 점이 늘어나 구조가 복잡해짐

Fragment 사용

 

1.Fragmnet.java

package com.example.fragmnet_ex;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment extends androidx.fragment.app.Fragment {

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

LayoutInflater inflater - fragment의 view를 확장하기 위해 사용되는 layoutInflater개체

ViewGroup container - null이 아니면 프래그먼트의 UI가 연결되어야 하는 상위 뷰로,

                                     프래그먼트는 뷰 자체를 추가해서는 안 되지만 뷰의 LayoutParam을 생성하는 데 사용

Bundle savedInstanceState - null이 아니면 프래그먼트가 이전에 저장된 상태에서 재구성

 

return inflater.inflater( R.layout.activity_frag1,container,flase)

ㄴ 현재 뷰에 다른 뷰를 추가할 필요가 없기 때문에 false 를 사용

 

2.activity_frag

<?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:orientation="vertical"
    android:background="#ff0000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="Fragment1"/>


</LinearLayout>

3.activity_main.xml

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <fragment
        android:name="com.example.fragmnet_ex.Fragment"
        android:id="@+id/frag1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text"
            android:textSize="30dp"
            android:layout_weight="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="button"
            android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>

실행화면