现在我使用kotlin作为我的主要编程语言我试图简化Android的Recyclerview实现逻辑,并且我坚持以下几点:
假设我已经定义了以下ViewHolder类,其中点击位置事件通过构造函数中的方法引用传播。
class CustomViewHolder(itemView: View, val onClick: (Int) -> Unit)) : RecyclerView.ViewHolder(itemView){
itemView.setOnClickListener {
onClick.invoke(adapterPosition)
}
}
如何构建在构造函数中具有方法引用的泛型类的实例?
我的recyclerview实现在构造函数中有一个泛型类,还有一个实体引用,可以通过onCreateViewHolder的反射创建我的CustomViewHolder类的实例:
class CustomAdapter<HolderClassGeneric>(val entityClass: Class<HolderClassGeneric>) : RecyclerView.Adapter<HolderClassGeneric>() {
/*
Usual recyclerview stuff
*/
//Method that I want to be called when a list item is clicked
fun onListItemClicked(position: Int) {
//do stuff
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HolderClassGeneric {
val view = LayoutInflater.from(parent.context).inflate(listItemLayoutResourceId, parent, false)
val constructor: Constructor<HolderClassGeneric>
val instance: HolderClassGeneric
//I'M STUCK HERE
constructor = entityClass.getConstructor(View::class.java, ?¿?¿?¿?¿)
instance = constructor.newInstance(view, this::onListItemClicked)
return instance
}
如果无法实现我想要实现的目标,是否可以通过反射使用setter传递方法引用?
答案 0 :(得分:3)
Kotlin lambda通常被翻译为FunctionX对象,X是它拥有的参数数量,所以如果你要查找像(Int) -> Unit
这样的lambda函数的表示,它将是{{1所以你可以这样做:
Function1<Int, Unit>
答案 1 :(得分:0)
您可以使用参数功能,例如 factory 。
class CustomAdapter<HolderClassGeneric>(val entityFactory: (View, (Int) -> Unit) -> HolderClassGeneric) : RecyclerView.Adapter<HolderClassGeneric>() {
/*
Usual recyclerview stuff
*/
//Method that I want to be called when a list item is clicked
fun onListItemClicked(position: Int) {
//do stuff
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HolderClassGeneric {
val view = LayoutInflater.from(parent.context).inflate(listItemLayoutResourceId, parent, false)
val instance = entityFactory(view, this::onListItemClicked)
return instance
}