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?
activity.xml:?