通过数据绑定,如何将函数(或 lambda)变量传递给包含的布局

问题描述 投票:0回答:3

我将编辑器操作设置为编辑文本。 我的第一个代码是

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

这个效果很好

但是当我使用 include 标签时,我不知道如何将

@{vm::searchAction}
作为变量传递,如下所示:

活动.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>

我的视图模型:

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

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

有什么办法可以解决这个问题吗?

android xml kotlin include android-databinding
3个回答
16
投票

这是一个老问题,但我为其他需要帮助的人回答。

尝试像这样的代码。

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

<import type="kotlin.Unit" />

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

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

如果你的函数有很多参数,你可以这样尝试。

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

<import type="kotlin.Unit" />

<variable
    name="action"
    type="Function2&lt;Integer, String, Unit>"/>

0
投票

无需进口的替代品

您可以使用 java 的 Runnable 接口作为类型,而不是使用 kotlin 函数类型。

<include 
  layout="@layout/my_layout"
  action="@{() -> viewModel.myFunction()}"/>

mylayout.xml

<data>
  <variable
      name="action"
      type="Runnable" />
</data
<Button
android:onClick="@{() -> action.run()}"/>

-1
投票

试试这个: 活动.xml:

          ......

              <include
                    android:id="@+id/includedInput"
                    layout="@layout/view_input_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    />
            .....

view_input_layout.xml:

<data>
    <variable
        name="text"
        type="String" />

    <variable
        name="vm"
        type="com.example.MyViewModel" />   

</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:text="@{text}"
           app:onEditorAction="@{vm::searchAction}"
             ... />

        <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>

现在在您的活动中,您可以通过

view_input_layout.xml
id(来自 Activity.xml)访问
includedInput

在您的活动中初始化 viewModel,如下所示

includedInput?.vm = viewModel
inlcudedInput?.text = "hello this is your text"
© www.soinside.com 2019 - 2024. All rights reserved.