这是我的BindingAdapter:
@BindingAdapter(value = *arrayOf("bind:commentsAdapter", "bind:itemClick", "bind:avatarClick", "bind:scrolledUp"), requireAll = false)
fun initWithCommentsAdapter(recyclerView: RecyclerView, commentsAdapter: CommentsAdapter,
itemClick: (item: EntityCommentItem) -> Unit,
avatarClick: ((item: EntityCommentItem) -> Unit)?,
scrolledUp: (() -> Unit)?) {
//Some code here
}
Run Code Online (Sandbox Code Playgroud)
initWithCommentsAdapter 是一个顶级功能
这是我的布局(必不可少的部分):
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="some.example.path.CommentsViewModel"/>
<variable
name="commentsAdapter"
type="some.example.path.CommentsAdapter"/>
</data>
<android.support.v7.widget.RecyclerView
...
bind:avatarClick="@{(item) -> viewModel.avatarClick(item)}"
bind:itemClick="@{viewModel::commentClick}"
bind:commentsAdapter="@{commentsAdapter}"
bind:isVisible="@{viewModel.commentsVisibility}"
bind:scrolledUp="@{() -> viewModel.scrolledUp()}"
/>
</layout>
Run Code Online (Sandbox Code Playgroud)
当我在布局中使用kotlin方法调用分配lambda时,我在构建期间出现了这样的错误:
e: java.lang.IllegalStateException: failed to analyze:
java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:cannot find …Run Code Online (Sandbox Code Playgroud) 我在 android 中使用 DataBinding 并且我有一个自定义视图:CarouselView
我为此编写了一个绑定适配器:
@BindingAdapter("onClick")
fun setOnClick(carouselView: CarouselView, onClick: (position: Int) -> Unit) {
carouselView.setImageClickListener { position ->
onClick.run(position)
}
}
Run Code Online (Sandbox Code Playgroud)
在 xml 中:
<com.synnapps.carouselview.CarouselView
android:id="@+id/carouselView"
...
app:onClick="@{(p) -> vm.onAdsClicked(p)}"/>
Run Code Online (Sandbox Code Playgroud)
但它不编译。所以我在 Stackoverflow 中看到了这个答案。但我的问题是我不能使用 Runnable 而不是 kotlin hoc 函数,因为我需要传递一个参数来运行。
我该如何解决?
android higher-order-functions kotlin android-databinding android-binding-adapter