Jes*_*ehj 5 xml android include kotlin android-databinding
我将编辑器操作设置为 edittext。我的第一个代码是
@BindingAdapter("onEditorAction")
fun bindOnEditorAction(view: TextView, event: () -> Unit) {
view.setOnEditorActionListener { _, _, _ ->
event()
true
}
}
Run Code Online (Sandbox Code Playgroud)
...
<EditText
...
app:onEditorAction="@{vm::searchAction}"
... />
Run Code Online (Sandbox Code Playgroud)
这运作良好
但是当我使用 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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我的视图模型:
class MyViewModel(): ViewModel() {
val input = ObservableField<String>()
fun searchAction() {
Log.i("searchAction", "$input")
}
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以解决这个问题吗?
Woo*_*Lee 16
这是一个老问题,但我为其他需要帮助的人回答。
尝试像这样的代码。
<import type="kotlin.jvm.functions.Function0" />
<import type="kotlin.Unit" />
<variable
name="action"
type="Function0<Unit>" />
...
android:onClick="@{() -> action.invoke()}"
...
Run Code Online (Sandbox Code Playgroud)
如果你的函数有很多参数,你可以尝试这样。
<import type="kotlin.jvm.functions.Function2" />
<import type="kotlin.Unit" />
<variable
name="action"
type="Function2<Integer, String, Unit>"/>
Run Code Online (Sandbox Code Playgroud)
cha*_*ohd -1
试试这个:activity.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"
/>
.....
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
现在在您的活动中您可以view_input_layout.xml通过includedInputid 访问(来自 Activity.xml)
在您的活动中初始化 viewModel 像这样
includedInput?.vm = viewModel
inlcudedInput?.text = "hello this is your text"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5236 次 |
| 最近记录: |