[안드로이드] View Binding(뷰바인딩) 사용 방법

Lee Jun Hyeong·2023년 5월 3일
0

Android 톺아보기 😇

목록 보기
14/17
post-thumbnail

이 포스팅에서는 View BindingActivityFragment의 예제를 다룰 것이다.
이론이 필요한 사람은 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
    }


}
profile
"왜" 사용하며, "어떻게" 사용하는지에 대해

0개의 댓글