layout include한 상황에서 bindingAdapter 사용하기

나고수·2022년 5월 2일
0

1일1공부

목록 보기
31/68
//include 될 layout - container_juso_search.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable 👈 include될 & include할 레이아웃 둘다 variable 지정 
            name="viewModel2" 
            type="com.eundmswlji.tacoling.ui.map.MapViewModel" />
    </data>

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/bg_white_4_stroke_sub_red"
            android:gravity="center"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:textColor="@color/black"
            android:textSize="14sp"
            app:currentAddress="@{viewModel2.currentAddress.peekContent()}"/> 👈 Event Livedata라 peekContent()해서 data 꺼내옴
</layout>
//위의 xml을 include한 main layout
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bind="http://schemas.android.com/apk/res-auto"> 👈

    <data>
        <variable 👈 include될 & include할 레이아웃 둘다 variable 지정 
            name="viewModel"
            type="com.eundmswlji.tacoling.ui.map.MapViewModel" />
    </data>

            <include
                bind:viewModel2="@{viewModel}" 👈 include된 레이아웃의 variable(여기서는 viewModel2)을 여기서 넘겨줌 
                android:id="@+id/tvJuso"
                layout="@layout/container_juso_search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                app:layout_constraintBottom_toTopOf="@id/tvNoData"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
</layout>          
object BindingAdapter {
    @BindingAdapter("currentAddress")
    @JvmStatic
    fun setCurrentAddress(editText: EditText, address: String?) {
        address?.let { editText.setText(address) }
    }
}
profile
되고싶다

0개의 댓글