简体   繁体   中英

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.

Module app build.gradle I've added this

android {
    ...
    dataBinding {
        enabled = true
    }
}

User class

@Parcelize
data class User(var first: String, var last: String): Parcelable

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

<?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

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:

  1. Because you used DataBinding in fragment_form.xml, the corresponding FragmentFormBinding class will be automatically generated

  2. We usually use XxxBinding.inflate instead of inflater.inflate (layoutid, container, false)

  3. After FragmentFormBinding.inflate, we bind the user object to fragment_form.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM