티스토리 뷰
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;
}
});
}
}



'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
링크
TAG
- XML
- JSONObject
- DataBinding
- Vue.js #Vue.js + javascript
- ViewModel
- activity
- 안드로이드
- Livedata
- MVVM
- handler
- CoordinatorLayout
- java
- View
- 코틀린
- Android
- James Kim
- recyclerview
- coroutine
- Intent
- JSONArray
- Architecture Pattern
- ArrayList
- fragment
- Design Pattern
- 자바
- ViewPager2
- Kotlin
- 혀가 길지 않은 개발자
- TabLayout
- 안드로이드 #코틀린 #Android #Kotlin
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함