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
๋ฐ์ํ