이 포스팅에서는 View Binding
의 Activity
와 Fragment
의 예제를 다룰 것이다.
이론이 필요한 사람은 ViewBinding vs DataBinding 참고
android {
.
..
// 해당 코드 추가
buildFeatures{
viewBinding = true
}
}
<?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>
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="변경된 텍스트입니다."
}
}
새로운 액티비티 SecondActivity
를 만든다.
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>
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에 View Binding 설정하기
<?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>
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
}
}