Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With data binding, How to pass function(or lambda) variable to included layout

I will set editor action to edittext. My first code is

@BindingAdapter("onEditorAction")
fun bindOnEditorAction(view: TextView, event: () -> Unit) {
    view.setOnEditorActionListener { _, _, _ ->
        event()
        true
    }
}
...
    <EditText
        ...
        app:onEditorAction="@{vm::searchAction}"
        ... />

This is working well

but when I use include tag, I don't know how to pass @{vm::searchAction} as variable like this:

activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<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
            name="vm"
            type="com.example.MyViewModel" />
    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:fillViewport="true"
        android:overScrollMode="never">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

           
            <include
                android:id="@+id/input"
                layout="@layout/view_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:action="@{vm::searchAction}"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:text="@={vm.input}" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</layout>

view_input_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<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
            name="text"
            type="String" />

        <variable
            name="action"
            type="???" />   // **What type for event??**    

    </data>

    <com.google.android.material.card.MaterialCardView
            android:id="@+id/input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/input_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:text="@{text}
                app:editorAction=@{action} />  // Passed action will be setted here!

            <TextView
                android:id="@+id/timer_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|end"
                android:layout_marginEnd="20dp"
                tools:text="00:00" />

        </com.google.android.material.card.MaterialCardView>

</layout>

MyViewModel:

class MyViewModel(): ViewModel() {
    
    val input = ObservableField<String>()

    fun searchAction() {
        Log.i("searchAction", "$input")
    }
}

Is there any way to solve this problem?

like image 311
Jessehj Avatar asked Aug 30 '26 18:08

Jessehj


1 Answers

It's an old question, but I answer for others who need help.

Try like this code.

<import type="kotlin.jvm.functions.Function0" />

<import type="kotlin.Unit" />

<variable
    name="action"
    type="Function0&lt;Unit>" />

...
android:onClick="@{() -> action.invoke()}"
...

If your function has many params, you can try like this.

<import type="kotlin.jvm.functions.Function2" />

<import type="kotlin.Unit" />

<variable
    name="action"
    type="Function2&lt;Integer, String, Unit>"/>
like image 61
Woochan Lee Avatar answered Sep 01 '26 10:09

Woochan Lee