fragment_example.xmlstrings.xmldimens.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> <import type="android.view.View" /> <import type="com.codinginflow.databindingexample.User" /> <import type="java.util.List" /> <variable name="user" type="User" /> <variable name="friends" type="List<User>" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".ExampleFragment"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@{@dimen/padding1 + @dimen/padding2}" android:text="@{@string/hello(user.nickName ?? user.firstName)}" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/text_view_first_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.firstName, default = `First Name`}" 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, default = "Last Name"}' 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) + ` years old`}" android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textColor="@{user.age < 18 ? @android:color/holo_red_light : @android:color/black}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{@plurals/friends(friends.size(), friends.size())}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{friends[0].firstName}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{friends[1].firstName}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{friends[2].firstName}" /> </LinearLayout> </layout>
<resources> <string name="app_name">Data Binding Example</string> <string name="hello">Hello, %s</string> <plurals name="friends"> <item quantity="one">You have %d friend</item> <item quantity="other">You have %d friends</item> </plurals> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="padding1">10dp</dimen> <dimen name="padding2">6dp</dimen> </resources>
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, "Flow") val friends = listOf( User("Gudrun", "Schulze", 65) ) binding.friends = friends 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, val nickName: String? = null )