-
data binding : providing values in Include layoutAndroid 2021. 11. 5. 08:46
Data object
data class Location( @field:SerializedName("name") val name:String, )
Binding Data
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding: ActivityMainBinding = DataBindingUtil.setContentView( this, R.layout.activity_main) binding.location = Location("Seoul") }
Includes
layout xml에 xmlns:bind="http://schemas.android.com/apk/res-auto"를 추가한다.
main 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"> <data> <variable name="location" type="test.com.Location" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/current_weather" bind:location="@{location}"/> </LinearLayout> </layout>
include layout
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="location" type="test.com.Location" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{location.name}"/> </LinearLayout> </layout>
Reference
'Android' 카테고리의 다른 글
Managing Threads and Custom Services (0) 2022.02.04 WorkManager(1) (0) 2021.10.04 Fragment (0) 2021.08.16 Replace findViewById with View Binding (0) 2021.08.06 ANR(Application Not Responding) (0) 2021.07.19