【发布时间】:2017-07-10 11:00:28
【问题描述】:
这是我的 BindingAdapter
@InverseBindingMethods({
@InverseBindingMethod(type = RecyclerView.class, attribute = "lastAdapter", method = "getLastAdapter"),
})
public class RecyclerViewBindingAdapter {
@BindingAdapter(value = {"lastAdapter"}, requireAll = false)
public static void setLastAdapter(RecyclerView view, LastAdapter adapter) {
view.setAdapter(adapter);
}
@InverseBindingAdapter(attribute = "lastAdapter")
public static LastAdapter getLastAdapter(RecyclerView view) {
return (LastAdapter) view.getAdapter();
}
}
我在我的 gradle 上添加了 RecyclerView 依赖项:
compile "com.android.support:recyclerview-v7:25.3.1"
这是我在布局中的使用方式:
<android.support.v7.widget.RecyclerView
app:lastAdapter = "@{viewModel.adapter}"
... />
我已经在扩展此布局后设置了 viewModel 绑定变量。但是每当我运行我的代码时,gradle 总是显示以下错误
Error:(48, 34) Cannot find the setter for attribute 'app:lastAdapter' with parameter type error.NonExistentClass on android.support.v7.widget.RecyclerView.
我可以通过从我的布局中删除任何app:lastAdapter 然后清理并重建,再次重写那些app:lastAdapter 属性然后重建项目来解决这个问题,一切正常。
这不是一个方便的解决方案。
【问题讨论】:
-
你的变量行是什么样的?你的 ViewModel 中的
adapter是什么? -
嗨 George,
adapter是来自 LastAdapter 类的对象,这里是var adapter = LastAdapter(data, BR.item).map<MenuLinked, MenuItemBinding>(R.layout.menu_item) -
您的 BindingAdapter 采用
LastAdapter,但适配器似乎是不同的类型。我读对了吗? -
看看
adapter被隐式声明为LastAdapter。可能是因为类型没有明确声明吗?让我试试 -
谢谢乔治。就是这样,由于Kotlin隐式类型赋值,BindingAdapter无法确定类型
标签: android android-databinding android-binding-adapter