fragment_example.xmlfragment_example.xml (land)age.xmlExampleFragment.ktactivity_main.xmlMainActivity.ktUser.kt
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="user" type="com.codinginflow.databindingexample.User" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".ExampleFragment"> <TextView android:id="@+id/text_view_first_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.firstName}" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/text_view_last_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.lastName}" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/text_view_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{String.valueOf(user.age)}" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> </LinearLayout> </layout>
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="user" type="com.codinginflow.databindingexample.User" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".ExampleFragment"> <TextView android:id="@+id/text_view_first_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.firstName}" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/text_view_last_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.lastName}" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <include android:id="@+id/include_layout" layout="@layout/age" bind:userIncluded="@{user}" /> </LinearLayout> </layout>
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="userIncluded" type="com.codinginflow.databindingexample.User" /> </data> <TextView android:id="@+id/text_view_age_included" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{String.valueOf(userIncluded.age)}" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> </layout>
package com.codinginflow.databindingexample import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import com.codinginflow.databindingexample.databinding.FragmentExampleBinding class ExampleFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val binding = FragmentExampleBinding.inflate(inflater, container, false) binding.user = User("Florian", "Walther", 29) binding.includeLayout?.textViewAgeIncluded return binding.root } }
<?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" tools:context=".MainActivity"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
package com.codinginflow.databindingexample import androidx.appcompat.app.AppCompatActivity import android.os.Bundle class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) supportFragmentManager.beginTransaction().replace(R.id.fragment_container, ExampleFragment()).commit() } }
package com.codinginflow.databindingexample data class User(val firstName: String, val lastName: String, val age: Int)