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_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_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>
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>
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;
}
});
}
}