[アンドロイド]Fragment ViewBinding


~Fragment1~
<androidx.constraintlayout.widget.ConstraintLayout 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="My_Library.SOPT_Fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:background="@color/purple_200"/>

</androidx.constraintlayout.widget.ConstraintLayout>
~Fragment2~
<androidx.constraintlayout.widget.ConstraintLayout 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="My_Library.SOPT_Fragment2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:background="#00BCD4"/>

</androidx.constraintlayout.widget.ConstraintLayout>
~Fragment1 액티비티~
package My_Library

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.kotlin_study.databinding.FragmentSOPTBinding

class SOPT_Fragment1 : Fragment() {
    private var _binding: FragmentSOPTBinding? = null
    private val binding get() = _binding!!
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _binding = FragmentSOPTBinding.inflate(layoutInflater, container, false)
        return binding.root
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }
}
~Fragment2 액티비티~
package My_Library

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.kotlin_study.databinding.FragmentSopt2Binding


class SOPT_Fragment2 : Fragment() {
    private var _binding: FragmentSopt2Binding? = null  //binding 파일 이름이 Activity로 시작하지 않을 수 있네
    private val binding get() = _binding!!
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _binding= FragmentSopt2Binding.inflate(layoutInflater,container,false)
        return binding.root
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }
}
~메인 엑티비티~
package My_Library

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlin_study.R
import com.example.kotlin_study.databinding.ActivitySoptFragmentBinding

class SOPT_Fragment_Activity : AppCompatActivity() {
    private lateinit var binding: ActivitySoptFragmentBinding
    private var position = FIRST_POSITION

    companion object {
        const val FIRST_POSITION = 1
        const val SECOND_POSITION = 2
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySoptFragmentBinding.inflate(layoutInflater)
        setContentView(binding.root)

        transactionFragment()
    }


   private fun transactionFragment() {
        val fragment1 = SOPT_Fragment1()
        val fragment2 = SOPT_Fragment2()

        binding.bvFragment.setOnClickListener {
            //setonclicklistener코드 밖에썼더니 안됨: transaction호출할때마다 beginrtansaction얻어야하는군
            val transaction = supportFragmentManager.beginTransaction()
            when (position) {
                FIRST_POSITION -> {
                    transaction.replace(R.id.container, fragment1)
                    position = SECOND_POSITION //포지션 바꿔주는거 잊지말기
                }
                SECOND_POSITION -> {
                    transaction.replace(R.id.container, fragment2)
                    position = FIRST_POSITION
                }

            }
            transaction.commit()  //transaction사용후 항상 commit()으로 닫기
        }

    }

}