Fragment
์กํฐ๋นํฐ์ ์ข ์๋์ด ์๋ ํํธ ์กฐ๊ฐ
์กํฐ๋นํฐ ํ ํ๋ฉด์์ ์กํฐ๋นํฐ ๊ฐ์ ์ด๋์ ๊ฑฐ์น์ง ์๊ณ
์กํฐ๋นํฐ์์ ์ฌ๋ฌ ๊ฐ์ ํํธ์ ๊ฐ์ ๊ปด์ฃผ๋ ๋ฐฉ์์ผ๋ก์ ํ๋ฉด ๊ตฌ์ฑ
[ ๋ ์ด์์ ํ์ผ ]
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ํ๋๊ทธ๋จผํธ1" />
<Button
android:id="@+id/btn_fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ํ๋๊ทธ๋จผํธ2" />
<Button
android:id="@+id/btn_fragment3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ํ๋๊ทธ๋จผํธ3" />
</LinearLayout>
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp" />
</LinearLayout>
fragment1.xml
, fragment2.xml
, fragment3.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#FFCACA">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ํ๋๊ทธ๋จผํธ1"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
![]() | ![]() | ![]() |
[ ์ฝํ๋ฆฐ ํ์ผ ]
Fragment1.kt
, Fragment2.kt
, Fragment3.kt
FragmentA ๋ Frgment ํด๋์ค๋ก๋ถํฐ ์์๋ฐ์
onCreateView
-> Fragment ํ๋ฉด์ ๊ตฌ์ฑํ ๋ ํธ์ถ๋๋ ์ฝ๋ฐฑ ๋ฉ์๋
package com.example.myfragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class Fragment1 : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment1, container, false)
return view
}
}
MainActivity.kt
package com.example.myfragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.example.myfragment.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val btnArr : Array<Button> = arrayOf(binding.btnFragment1, binding.btnFragment2, binding.btnFragment3)
btnArr.forEachIndexed { index, b ->
b.setOnClickListener {
setFrag(index)
}
}
}
private fun setFrag(fragNum: Int) {
val ft = supportFragmentManager.beginTransaction()
when (fragNum) {
0 -> {
ft.replace(R.id.main_frame, Fragment1()).commit()
}
1 -> {
ft.replace(R.id.main_frame, Fragment2()).commit()
}
2 -> {
ft.replace(R.id.main_frame, Fragment3()).commit()
}
}
}
}