Kotlin数据绑定与扩展方法 [英] Kotlin databinding with extension methods
问题描述
我正在尝试在Android的数据绑定中使用Kotlin扩展方法.例如;调用onclick处理程序.因此,我编写了以下代码:
I'm trying to use Kotlin extension methods inside Android's databinding. For example; calling an onclick handler. So I've made this code:
posttest_list_item.xml
posttest_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<import type="android.view.View"/>
<import type="com.example.test.post.posttest.PostTestItemViewModelExtensionKt" />
<variable
name="viewModel"
type="com.example.test.post.posttest.PostTestItemViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:clickable="true"
android:onClick="@{(view) -> viewModel.clicked(view)}"
>
[...]
PostTestItemViewModel.kt
PostTestItemViewModel.kt
open class PostTestItemViewModel : ViewModel() {
val postTitle = MutableLiveData<String>()
val postBody = MutableLiveData<String>()
/**
* Binds the required properties/entities to this ViewModel
*/
fun bind(post: Post) {
postTitle.value = post.title
postBody.value = post.body
}
}
PostTestItemViewModelExtension.kt
PostTestItemViewModelExtension.kt
fun PostTestItemViewModel.clicked(v: View) {
this.postTitle.value = "clicked"
}
因此,当我将clicked方法放置在viewmodel内时,它可以按应有的方式完美地工作.但是,当我将其创建为扩展方法时,在编译时会出现以下错误:
So when I place the clicked method inside the viewmodel, it works perfectly the way it should be. However, when I create it as an extension method, I get the following error on compilation:
e:[kapt]发生了异常:android.databinding.tool.util.LoggedErrorException:发现了数据绑定错误. 无法在类... PostItemViewModel
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. cannot find method clicked(android.view.View) in class ...PostItemViewModel
我已经尝试过其他方法,例如将android:onclick
标记更改为PostTestItemViewModelExtensionKt
而不是viewModel
.不幸的是,所有事情似乎都不起作用.因此,看起来像是在数据绑定发生之后生成了扩展方法.有办法解决还是我做错了什么?还是无法绑定扩展方法?
I've tried different things already, such as changing the android:onclick
tag to PostTestItemViewModelExtensionKt
instead of viewModel
. Unfortunately all the things don't seem to work. So it looks like the extension method is getting generated after the databinding takes place. Is there a way around this or am I still doing something wrong? Or is it just not possible to bind extension methods?
我正在使用Kotlin版本1.2.71,gradle 3.2.0,并将databinding { enabled = true }
和kapt { generateStubs = true }
添加到我的.gradle中,并定义了插件kotlin-android
,kotlin-android-extensions
和kotlin-kapt
.
I'm using Kotlin version 1.2.71, gradle 3.2.0 and have the databinding { enabled = true }
and kapt { generateStubs = true }
added to my .gradle, and have the plugings kotlin-android
, kotlin-android-extensions
and kotlin-kapt
defined.
推荐答案
不幸的是,您不能将扩展方法用作onClick
回调.
Unfortunately you can't use extension methods as onClick
callbacks.
Kotlin中的扩展方法是作为Java静态方法创建的,而Android框架则需要一个实例方法.
Extension methods in Kotlin are created as Java static methods while the Android framework is expecting an instance method.
请注意,在Android Studio中,您可以将Kotlin类反编译为Java,以查看生成的Java代码.
Note that in Android Studio you can decompile the Kotlin classes as Java to see the generated Java code.
这篇关于Kotlin数据绑定与扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!