[Android] 프래그먼트(Fragment) - (1)설명, (2)첫번째 방법(xml레이아웃에 추가하는 방법)

[Android] 프래그먼트(Fragment) - (3)두번째 방법(java소스 코드를 이용해서 추가하는 방법)

[Android] 프래그먼트(Fragment) - (4)프래그먼트 2개를 번갈아가며 보여지도록 만듬

[Android] 프래그먼트(Fragment) - (5)프래그먼트 안에 있는 버튼을 눌렀을 때,다른 프래그먼트를 띄우는 방법

[Android] 프래그먼트(Fragment) - (6)실습(왼쪽의 리스트에서 하나 선택하면 오른쪽에 이미지 보기)

[Android] 프래그먼트(Fragment) - (7)데이터 이동(간단 정리) ★

 

 

 

 

 

 

 

 

 

 

https://hijjang2.tistory.com/253

이 블로그 수정해서 작업했음.

 

 

 

 

 

 

1. 코드 수정

1) 프래그먼트 하나 더 만들어줌

(MainFragment.java, fragment_main.xml 복사해서

MenuFragment.java, fragment_menu.xml 만들어 주고)

 

 

2) activity_main.xml에다가 "시작"버튼을 -> "메인", "메뉴" 버튼으로 바꿔준다.

 

 

 

3) MainActivity.java 코드 수정

replace 사용해서 프래그먼트를 대체(바꿀)할 수가 있다.

 

 

 

 

 

 

2. 결과

 

 

 

 

 

 

3. 구조

 

 

 

 

 

4. 전체 코드

-MainActivity.java

public class MainActivity extends AppCompatActivity {
    MainFragment fragment1;
    MenuFragment fragment2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragment1 = new MainFragment();
        fragment2 = new MenuFragment();

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment1).commit();
            }
        });

        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment2).commit();
            }
        });
    }
}

-MainFragment

public class MainFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_main,container,false);

        return rootView;
    }
}

-MenuFragment

public class MenuFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_menu,container,false);

        return rootView;
    }
}

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

    <Button
        android:id="@+id/button"
        android:text="메인"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button2"
        android:text="메뉴"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <FrameLayout
        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.example.fragment1.MainFragment"/>-->
    </FrameLayout>

</LinearLayout>

-fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF9800">
    <TextView
        android:textSize="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="프래그먼트 1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="메뉴화면으로"/>

</LinearLayout>

-fragment_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00BCD4">
    <TextView
        android:textSize="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="프래그먼트 2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="메인화면으로"/>

</LinearLayout>