Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//inline fun <reified TV : View> v(context: Context, init: TV.() -> Unit) : TV {
inline fun <reified TV : View> Context.v(init: TV.() -> Unit) : TV {
val constr = TV::class.java.getConstructor(Context::class.java)
// val view = constr.newInstance(context)
val view = constr.newInstance(this)
view.init()
return view
}
//inline fun <reified TV : View> v(parent: ViewGroup, init: TV.() -> Unit) : TV {
inline fun <reified TV : View> ViewGroup.v(init: TV.() -> Unit) : TV {
val constr = TV::class.java.getConstructor(Context::class.java)
// val view = constr.newInstance(parent.context)
val view = constr.newInstance(context)
parent.addView(view)
view.init()
return view
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.