PROGRAMMING/Android
[JETPACK_Android View] ViewBinding
yuujoeng
2023. 3. 5. 00:22
1 ViewBinding 특징
- Null 안전성 : 뷰의 직접 참조를 생성하지 않으므로 뷰 ID 부재에 의한 null 포인터 예외가 발생하지 않음
- 유형 안전성 : 각 바인딩 클래스에 있는 필드 유형이 XML 파일에서 참조하는 뷰와 일치하므로 클래스 변환 예외가 발생하지 않음
2 Activity
// build.gradle에 데이터 바인딩 추가 !
buildFeatures{
viewBinding = true
}
// 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/testText"
android:textSize="50sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
// MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/* ViewBinding - Activity */
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
/* 데이터 바인딩을 사용해 텍스트 값 변경 */
binding.testText.text = "변경된 텍스트"
}
}


2 Fragment
//activity_.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"
tools:context=".Activity">
<FrameLayout
android:id="@+id/frameArea"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
// Activity.kt
// 프레그먼트 사용을 위한 FragmentManager 세팅
class Activity : AppCompatActivity() {
val manager = supportFragmentManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_)
val transaction = manager.beginTransaction()
val fragment = TestFragment()
transaction.replace(R.id.frameArea, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}
//TestFragment.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="hello_blank_fragment" />
</FrameLayout>
// TestFragment.kt
class TestFragment : Fragment() {
//dataBinding 설정
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? {
// DataBinding을 사용해 fragmentText의 text 변경 !
_binding = FragmentTestBinding.inflate(inflater, container, false)
val view = binding.root
binding.fragmentText.text = "fragment Text"
return view
}
}

