[英]Minimum databinding example with kotlin
I went through couple of examples but either they have too many abstractions or out of date tutorials.我经历了几个例子,但要么他们有太多的抽象或过时的教程。 I would like to implement this feature in the minimum possible way.我想以尽可能少的方式实现这个功能。 I need help with the UserForm class here with User
passed as arguments in a Navigational Architecture.我需要这里的 UserForm 类的帮助,其中User
作为导航架构中的参数传递。
Module app build.gradle I've added this模块 app build.gradle我已经添加了这个
android {
...
dataBinding {
enabled = true
}
}
User class用户类
@Parcelize
data class User(var first: String, var last: String): Parcelable
FormFragment.kt FormFragment.kt
class UserForm: Fragment() {
private val user by lazy {
arguments?.getParcelable("user") ?: User("John", "Doe")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_form, container, false)
}
}
fragment_form.xml fragment_form.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="user" type="data.User" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/first"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="@{user.first}" />
<EditText
android:id="@+id/last"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="@{user.last}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Some snippets一些片段
modify your code below:修改下面的代码:
FormFragment.kt FormFragment.kt
class UserForm: Fragment() {
private val user by lazy {
arguments?.getParcelable("user") ?: User("John", "Doe")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = FragmentFormBinding.inflate(inflater, container, false).apply {
user = this@UserForm.user
}
return binding.root
}
}
Explanation:解释:
Because you used DataBinding in fragment_form.xml, the corresponding FragmentFormBinding class will be automatically generated因为你在fragment_form.xml中使用了DataBinding,会自动生成对应的FragmentFormBinding类
We usually use XxxBinding.inflate instead of inflater.inflate (layoutid, container, false)我们通常使用 XxxBinding.inflate 而不是 inflater.inflate (layoutid, container, false)
After FragmentFormBinding.inflate, we bind the user object to fragment_form.xml在 FragmentFormBinding.inflate 之后,我们将用户对象绑定到 fragment_form.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.