앱개발

Fragment

카멜레온개발자 2021. 5. 5. 21:26

1. Fragment 생성(MainFragment, MenuFragment)

2. 다음의 창이 나타나면 이름 입력(TestFrament => MainFragment)

3. MainFragment는 MainActivity의 xml에 바로 등록한다. MainActivity는 FrameLayout으로 변경

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="org.hdongle.fragment.MainFragment"
        android:id="@+id/mainFragment">

    </fragment>
</FrameLayout>

4. 각각의 fragment들을 다음과 같이 바꾼다

 

* fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFEB3B"
    android:orientation="vertical"
    tools:context=".MainFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center|center_vertical"
        android:text="메인 프레그먼트"
        android:textSize="50sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFC107"
        android:text="화면전환" />
</LinearLayout>

* fragment_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000FF"
    android:orientation="vertical"
    tools:context=".MainFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center|center_vertical"
        android:text="메뉴 프레그먼트"
        android:textSize="50sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFC107"
        android:text="돌아가기" />
</LinearLayout>

 

5. Fragment들의 java에 다음과 같이 코딩한다

 

*MainFragment.java

package org.hdongle.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainFragment extends Fragment {

    //xml layout을 메모리에 올리는 역할을 한다.
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_main, container, false);
        ViewGroup vg = (ViewGroup)inflater.inflate(R.layout.fragment_main, container, false);
        Button button = vg.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mainActivity = (MainActivity)getActivity();
                mainActivity.OnFragmentChanged(1);
            }
        });

        return vg;
    }
}

 

* MenuFragment.java

package org.hdongle.fragment;

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

import androidx.fragment.app.Fragment;

public class MenuFragment extends Fragment {

    //xml layout을 메모리에 올리는 역할을 한다.
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_menu, container, false);

        Button button = rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity activity = (MainActivity)getActivity();
                activity.OnFragmentChanged(0);
            }
        });

        return rootView;
        //return inflater.inflate(R.layout.fragment_menu, container, false);
    }
}

 

6. MainActivity에 다음과 같이 코드를 넣어준다

* 핵심 코드 : container(FrameLayout)에 Fragment를 교체해준다

public class MainActivity extends AppCompatActivity {

    MainFragment mainFragment;
    MenuFragment menuFragment;

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

        mainFragment = (MainFragment)getSupportFragmentManager().findFragmentById(R.id.mainFragment);
        menuFragment = new MenuFragment();
    }

    public void OnFragmentChanged(int idx)
    {
        if(idx == 0)
        {
            getSupportFragmentManager().beginTransaction().replace(R.id.container, mainFragment).commit();
        }
        else if(idx == 1){
            getSupportFragmentManager().beginTransaction().replace(R.id.container, menuFragment).commit();
        }
    }
}