简体   繁体   English

如何使用kotlin创建在构造函数中传递方法引用的泛型类的实例

[英]How to create instance of generic class passing a method reference in constructor with kotlin

Now that I'm using kotlin as my main programing language I'm trying to simplify Android's Recyclerview implementation logic and I'm stuck at following point: 现在我正在使用kotlin作为我的主要编程语言,我正在尝试简化Android的Recyclerview实现逻辑,我坚持以下几点:

Let's say I have defined the following ViewHolder class, where a click position event is propagated via a method reference in the constructor. 假设我已经定义了以下ViewHolder类,其中点击位置事件通过构造函数中的方法引用传播。

class CustomViewHolder(itemView: View, val onClick: (Int) -> Unit)) : RecyclerView.ViewHolder(itemView){
    itemView.setOnClickListener {
        onClick.invoke(adapterPosition)
    } 
}

How could I build an instance of a generic class that has a method reference in the constructor? 我怎样才能构建一个在构造函数中有方法引用的泛型类的实例?

My recyclerview implementation has a generic class in the constructor and also an entity reference to be able to create an instance of my CustomViewHolder class by reflection at onCreateViewHolder: 我的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

}

If what I want to achieve is not possible, would it be possible to pass the method reference using a setter by reflection? 如果我想要实现的是不可能的,那么可以通过反射使用setter传递方法引用吗?

Kotlin lambda通常被翻译为FunctionX对象,X是它所拥有的参数的数量,所以如果你正在寻找像(Int) -> Unit这样的lambda函数的表示,那么它将是Function1<Int, Unit> so你可以这样做:

entityClass.getConstructor(View::class.java, Function1::class.java)

You can use a parameter function like a 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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM