티스토리 뷰

Android/Kotlin

[Kotlin]  Fragment

혀가 길지 않은 개발자 2020. 7. 1. 23:33

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:background="#FFAAAA">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Fragment Red"
        android:textSize="30dp"/>

</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:background="#AAFFAA">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Fragment Green"
        android:textSize="30dp"/>

</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:background="#AAAAFF">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Fragment Blue"
        android:textSize="30dp"/>

</LinearLayout>

fragment_blue.xml

 

 

 

 

 

FragmentRed.kt

package com.jwsoft.kotlinproject

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class FragmentRed : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_red, container, false)
    }
}

 

 

 

 

 

FragmentGreen.kt

package com.jwsoft.kotlinproject

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class FragmentGreen : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_green, container, false)
    }
}

 

 

 

 

 

FragmentBlue.kt

package com.jwsoft.kotlinproject

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class FragmentBlue : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <fragment
        android:id="@+id/fragmentRed"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="com.jwsoft.kotlinproject.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" />

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

</LinearLayout>

activity_main.xml

 

 

 

 

 

 

MainActivity.kt

package com.jwsoft.kotlinproject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction

class MainActivity : AppCompatActivity() {

    private lateinit var btnSwitch: Button
    private lateinit var fragmentGreen: FragmentGreen
    private lateinit var fragmentBlue: FragmentBlue

    private lateinit var fragmentManager: FragmentManager
    private lateinit var fragmentTransaction: FragmentTransaction
    private var isGreen = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fragmentGreen = FragmentGreen()
        fragmentBlue = FragmentBlue()

        fragmentManager = supportFragmentManager
        fragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.add(R.id.frameLayout, fragmentGreen)
        fragmentTransaction.commit()    // commit()은 한 번만 호출 가능

        btnSwitch = findViewById(R.id.btnSwitch)
        btnSwitch.setOnClickListener {

            if (isGreen) {
                fragmentTransaction = fragmentManager.beginTransaction()
                fragmentTransaction.replace(R.id.frameLayout, fragmentBlue)
                fragmentTransaction.commit()    // commit()은 한 번만 호출 가능
            } else {
                fragmentTransaction = fragmentManager.beginTransaction()
                fragmentTransaction.replace(R.id.frameLayout, fragmentGreen)
                fragmentTransaction.commit()    // commit()은 한 번만 호출 가능
            }
            isGreen = !isGreen

        }

    }
}

실행 결과
버튼 클릭 후

 

버튼 한 번 더 클릭 후

 

 

 

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

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