如何在Kotlin中创建化类型的对象

Sam*_*ert 2 android kotlin recycler-adapter kotlin-extension kotlin-reified-type-parameters

我正在尝试创建扩展功能以为回收站视图适配器创建视图持有者对象

inline fun <reified T: RecyclerView.ViewHolder> ViewGroup.createViewHolder(@LayoutRes res: Int): T {
    val inflater = LayoutInflater.from(context)
    val itemView = inflater.inflate(res, this, false)
    // return ViewHolder Object
}
Run Code Online (Sandbox Code Playgroud)

如何创建扩展RecyclerView.ViewHolder的T类型的对象,以便可以从函数返回。

NSi*_*mon 6

这个解决方案非常丑陋,但我假设“理论上”可以工作:

inline fun <reified T: RecyclerView.ViewHolder> ViewGroup.create(@LayoutRes res: Int): T {
        val inflater = LayoutInflater.from(context)
        val itemView = inflater.inflate(res, this, false)
        return T::class.java.getConstructor(View::class.java).newInstance(itemView)
    }
Run Code Online (Sandbox Code Playgroud)

最后一行的作用是: 1. 获取T匹配的构造函数T(view: View) 2. 调用newInstance该构造函数,将其传递给您膨胀的视图

解决方案改编自https://discuss.kotlinlang.org/t/generic-object-creation/1663/5

只需通过以下方式调用它:

val viewHolder = my_view_group.create<MyViewHolder>(R.layout.my_layout)
Run Code Online (Sandbox Code Playgroud)


Rob*_*ill 6

一个干净的替代解决方案是显式传递构造函数。它甚至不会更加冗长,因为可以推断出type参数,并且不再需要指定它。像这样使用:

val viewHolder = my_view_group.create(::MyViewHolder, R.layout.my_layout)
Run Code Online (Sandbox Code Playgroud)

像这样实现:

inline fun <reified T: RecyclerView.ViewHolder> ViewGroup.create(createHolder: (View) -> T, @LayoutRes res: Int): T {
    val inflater = LayoutInflater.from(context)
    val itemView = inflater.inflate(res, this, false)
    return createHolder(itemView)
}
Run Code Online (Sandbox Code Playgroud)

  • 你也可以用`::`(函数引用)来引用构造函数吗?我不知道,酷! (2认同)