[kotlin] TIL; Fragment 1

๊ฒฐยท2023๋…„ 12์›” 6์ผ
0

Mobile-Software

๋ชฉ๋ก ๋ณด๊ธฐ
2/15
post-thumbnail

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()
            }
        }
    }
}




0๊ฐœ์˜ ๋Œ“๊ธ€

Powered by GraphCDN, the GraphQL CDN