개발언어/JAVA

안드로이드 Fragment

우주먼지쪼가리 2020. 12. 25. 10:34
반응형

fragment : 하나의 액티비티여러 개의 화면을 가지도록 만들기위해 고안된 개념

@보통의 애플리케이션 설정키를 누르면 들어있는 수많은 버튼(화면설정, 디스플레이)중 하나를 누르면

나오는 layout. 그런 느낌.

 

Activity.java

package grocery.gohool.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

    private Button btn1,btn2,btn3,btn4,btn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn1 =(Button) findViewById(R.id.home1);
        btn2 =(Button) findViewById(R.id.home2);
        btn3 =(Button) findViewById(R.id.home3);
        btn4 =(Button) findViewById(R.id.home4);
        btn5 =(Button) findViewById(R.id.home5);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // FragmentManager에대한 참조회득, Transaction시작
                FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
                //생성
                Fragment1 fragment1 = new Fragment1();
                //교체
                fm.replace(R.id.frame,fragment1);
                fm.commit();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // FragmentManager에대한 참조회득, Transaction시작
                FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
                //생성
                Fragment2 fragment2 = new Fragment2();
                //교체
                fm.replace(R.id.frame,fragment2);
                fm.commit();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // FragmentManager에대한 참조회득, Transaction시작
                FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
                //생성
                Fragment3 fragment3 = new Fragment3();
                //교체
                fm.replace(R.id.frame,fragment3);
                fm.commit();
            }
        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // FragmentManager에대한 참조회득, Transaction시작
                FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
                //생성
                Fragment4 fragment4 = new Fragment4();
                //교체
                fm.replace(R.id.frame,fragment4);
                fm.commit();
            }
        });

        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // FragmentManager에대한 참조회득, Transaction시작
                FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
                //생성
                Fragment5 fragment5 = new Fragment5();
                //교체
                fm.replace(R.id.frame,fragment5);
                fm.commit();
            }
        });


    }
}

 

 

Activity _ xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <!-- Relative안에 있어서 LinearLayout을 alignParentBottom을 사용하여  Relative영역 내에서 움직일 수 있다.
        weight : 비율
    -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/home1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:text="홈"/>

        <Button
            android:id="@+id/home2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:text="마켓"/>
        <Button
            android:id="@+id/home3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:text="대화"/>
        <Button
            android:id="@+id/home4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:text="내상점"/>
        <Button
            android:id="@+id/home5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:text="설정"/>

    </LinearLayout>
</RelativeLayout>


fragment 1~5  .xml

 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="홈"
        android:textSize="30sp"/>

</FrameLayout>

fragment class

package grocery.gohool.fragment;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment {

    public Fragment1(){

    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return inflater.inflate(R.layout.fragment1,container,false);
    }
}

 

반응형