코딩; 2022. 2. 8. 08:36

 

1. app - src - main - res - layout폴더 우클릭 - 새로만들기 - XML - Layout XML File

   - activity_fragment_menu로 xml파일 생성

activity_fragment_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    
    <Button
            android:text="First"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button1"
            android:onClick="onClick"
            android:layout_weight="1"/>
    <Button
            android:text="Second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:onClick="onClick"
            android:layout_weight="1"/>
    <Button
            android:text="Third"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button3"
            android:onClick="onClick"
            android:layout_weight="1"/>

</LinearLayout>

 

2. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <fragment android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/fragment_menu"
              android:name="com.example.sample23.FragmentMenu"
              tools:layout="@layout/activity_fragment_menu"
              tools:ignore="MissingConstraints"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

3. app - src - main - java내 메인 패키지 파일 우클릭 - 새로만들기 - kotlin클래스/파일 - FragmentMenu로 생성

FragmentMenu.kt

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

 

4. activity_main.xml에 FrameLayout추가

<FrameLayout
        android:id="@+id/content"
        android:layout_width="598dp"
        android:layout_height="910dp"
        app:layout_constraintTop_toBottomOf="@+id/fragment_menu"
        app:layout_constraintStart_toStartOf="parent">
</FrameLayout>

 

5. app - src - main - res - layout폴더 우클릭 - 새로만들기 XML - Layout XML File

   - activity_fragment_one부터 three까지 생성(FrameLayout 안에  들어갈 화면을 생성하는 것임)

activity_fragment_one

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="#00ff00"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <TextView
            android:text="First Fragment"
            android:textSize="30sp"
            android:textColor="@color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_fragment_two

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="#0000ff"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <TextView
            android:text="Second Fragment"
            android:textSize="30sp"
            android:textColor="#999999"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    
</androidx.constraintlayout.widget.ConstraintLayout>

activity_fragment_three

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="#ff0000"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <TextView
            android:text="Third Fragment"
            android:textSize="30sp"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

6. app - src - main - java폴더 내 메인패키지 폴더 - 우클릭 - 새로만들기 - kotlin 클래스/파일

   - FragmentOne부터 Three까지 생성하고 Fragment상속받기

FragmentOne

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

FragmentTwo

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

FragmentThree

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

 

7. MainActivity.kt

class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val fm = supportFragmentManager
        val fragmentTransaction = fm.beginTransaction()
        fragmentTransaction.add(R.id.content, FragmentOne())
        fragmentTransaction.commit()
    }

    override fun onClick(v: View?) {
        Log.d("버튼 클릭!", "버튼 클릭!")

        var fr: Fragment? = null

        if(v?.id == R.id.button1){
            fr = FragmentOne()
        }else if(v?.id == R.id.button2){
            fr = FragmentTwo()
        }else if(v?.id == R.id.button3){
            fr = FragmentThree()
        }

        val fm = supportFragmentManager
        val fragmentTransaction = fm.beginTransaction()

        // fragment 교체
        fragmentTransaction.replace(R.id.content, fr!!)
        fragmentTransaction.commit()
    }
}