Dev/Android

include 의 레이아웃에 DataViewBinding 적용 참고

healthyryu 2021. 4. 6. 19:03

developer.android.com/topic/libraries/data-binding/expressions?hl=ko

 

레이아웃 및 결합 표현식  |  Android 개발자  |  Android Developers

표현식 언어를 사용하면 뷰에 의해 전달된 이벤트를 처리하는 표현식을 작성할 수 있습니다. 데이터 결합 라이브러리는 레이아웃의 뷰를 데이터 객체와 결합하는 데 필요한 클래스를 자동으로

developer.android.com

 

핵심은 xmlns:bind 를 추가해주는 부분이다.

 

Main Layout

<layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:bind="http://schemas.android.com/apk/res-auto">

...

<LinearLayout
           android:orientation="vertical"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
           <include layout="@layout/name"
               bind:user="@{user}"/>
           <include layout="@layout/contact"
               bind:user="@{user}"/>
       </LinearLayout>

</layout>

Include 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="user" type="com.example.User"/>
  </data>
  
.....

</layout>

 

MainLayout 의 bind:user 그리고 include layout 의 variable name="user" 와 같이 맞춰줘야 한다. 

반응형