티스토리 뷰

Android/Java

[Java]  Fragment

혀가 길지 않은 개발자 2020. 7. 2. 20:51

fragment_red.xml

<?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="#FFAAAA"
    android:gravity="center">

    <TextView
        android:id="@+id/txtViewRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment Green"
        android:textSize="30dp"
        android:textStyle="bold" />

</LinearLayout>

fragment_red.xml

 

 

 

 

fragment_green.xml

<?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="#AAFFAA"
    android:gravity="center">

    <TextView
        android:id="@+id/txtViewRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment Green"
        android:textSize="30dp"
        android:textStyle="bold" />

</LinearLayout>

fragment_green.xml

 

 

 

 

fragment_blue.xml

<?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="#AAAAFF"
    android:gravity="center">

    <TextView
        android:id="@+id/txtViewRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment Blue"
        android:textSize="30dp"
        android:textStyle="bold"/>

</LinearLayout>

fragment_blue.xml

 

 

 

 

 

FragmentRed.java

package com.jwsoft.javaproject;

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 FragmentRed extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_red, container, false);
    }
}

 

 

 

 

 

 

FragmentGreen.java

package com.jwsoft.javaproject;

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 FragmentGreen extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_green, container, false);
    }
}

 

 

 

 

 

 

 

FragmentBlue.java

package com.jwsoft.javaproject;

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 FragmentBlue extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blue, container, false);
    }
}

 

 

 

 

 

 

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">

    <fragment
        android:id="@+id/fragmentRed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:name="com.jwsoft.javaproject.FragmentRed"/>

    <Button
        android:id="@+id/btnSwitch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="Switch Green or Blue"
        android:textSize="18dp" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

activity_main.xml

 

 

 

 

 

 

MainActivity.java

package com.jwsoft.javaproject;

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

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

public class MainActivity extends AppCompatActivity {

    private Button btnSwitch;
    private FrameLayout frameLayout;
    private boolean isGreen = false;
    FragmentManager fm;

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

        btnSwitch = findViewById(R.id.btnSwitch);
        frameLayout = findViewById(R.id.frameLayout);

        fm = getSupportFragmentManager();

        btnSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isGreen) {
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.frameLayout, new FragmentGreen());
                    ft.commit();    // commit() 한 번만 사용 가능
                } else if (isGreen) {
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.frameLayout, new FragmentBlue());
                    ft.commit();    // commit() 한 번만 사용 가능
                }
                isGreen = !isGreen;
            }
        });

    }
}

실행 결과
버튼 클릭 후
버튼 한 번 더 클릭 후

 

 

'Android > Java' 카테고리의 다른 글

[Java]  TabLayout  (0) 2020.07.03
[Java]  FrameLayout  (0) 2020.07.02
[Java]  BottomSheetDialogFragment  (0) 2020.07.01
[Java]  BottomSheetDialog  (0) 2020.07.01
[Java]  SharedPreferences  +  Singleton Pattern  (0) 2020.06.30
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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 30
글 보관함