๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Android

[์•ˆ๋“œ๋กœ์ด๋“œ] View Binding(๋ทฐ๋ฐ”์ธ๋”ฉ) ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

by Jay Din 2023. 7. 17.
728x90
๋ฐ˜์‘ํ˜•

์ด ํฌ์ŠคํŒ…์—์„œ๋Š” View Binding์˜ Activity์™€ Fragment์˜ ์˜ˆ์ œ๋ฅผ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค.
// ์ด๋ก ์ด ํ•„์š”ํ•œ ์‚ฌ๋žŒ์€ ViewBinding vs DataBinding ์ฐธ๊ณ 

 

์„ธํŒ…

build.gradle

android {
	.
    ..
    // ํ•ด๋‹น ์ฝ”๋“œ ์ถ”๊ฐ€
    buildFeatures{
        viewBinding = true
    }
}
๋ฐ˜์‘ํ˜•

 

Activity

activity.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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:textSize="50sp"
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    // 1. ์ถ”๊ฐ€
    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // 2. ์ถ”๊ฐ€
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        
        // xml TextView Id๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ
        binding.textView.text="๋ณ€๊ฒฝ๋œ ํ…์ŠคํŠธ์ž…๋‹ˆ๋‹ค."
    }
}

Fragment

์ƒˆ๋กœ์šด ์•กํ‹ฐ๋น„ํ‹ฐ SecondActivity๋ฅผ ๋งŒ๋“ ๋‹ค.

Fragment๊นŒ์ง€ ๊ฐ€๋Š” ๊ณผ์ •

activity_second.xml

Fragment๋ฅผ ๋„ฃ์–ด์ค„ FrameLayout์„ ๋งŒ๋“ ๋‹ค.

<?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"
    tools:context=".SecondActivity">

    <FrameLayout
        android:id="@+id/frameArea"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

SecondActivity.kt

Fragment๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

class SecondActivity : AppCompatActivity() {

    private val manager = supportFragmentManager

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

        val transaction = manager.beginTransaction()
        val fragment = TestFragment()
        transaction.replace(R.id.frameArea, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }
}
๋ฐ˜์‘ํ˜•

 

Fragment ์„ค์ •

Fragment์— View Binding ์„ค์ •ํ•˜๊ธฐ

fragment_text.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".TestFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fragmentText"
        android:textSize="50sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

TestFragment

class TestFragment : Fragment() {

    // 1. ์ถ”๊ฐ€
    private var _binding: FragmentTestBinding ?= null
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // 2. ์ถ”๊ฐ€
        _binding = FragmentTestBinding.inflate(inflater, container, false)
        val view = binding.root

        binding.fragmentText.text = "์ด๊ฑฐ๋Š” Fragment Text"

        return view
    }


}

 

 

728x90
๋ฐ˜์‘ํ˜•