我目前正在使用数据绑定和mvvm开发一个Android应用程序。
我尝试将一个通用函数绑定到我的recyclerView但无法编译
布局:
<data>
<import type="android.view.View" />
<import type="com.app.Models.UserRole" />
<variable
name="usersViewModel"
type="com.app.UI.Users.UsersViewModel" />
</data>
-- ...
-- ...
-- ...
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewUsers"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_user"
android:background="@color/colorBackground"
app:adapter="@{<UserRole>usersViewModel.usersRoleList}" />
BindingAdapter类:
@BindingAdapter("adapter")
fun <T : BaseModel> addItems(recyclerView: RecyclerView, mutableList: MutableList<T>) {
val adapter = MasterAdapter<T>(arrayListOf(), { _, _ -> })
adapter.pushItems(mutableList)
recyclerView.layoutManager = LinearLayoutManager(recyclerView.context)
recyclerView.adapter = adapter
}
MasterAdapter类:
class MasterAdapter<T : BaseModel>(var items: MutableList<T>, private val listener: (T, Int) -> Unit)
BaseModel接口:
interface BaseModel {
@LayoutRes fun layoutId(): Int
}
我的问题是如何在布局中使用数据绑定将模型传递给我的泛型函数?