Skip to content

Instantly share code, notes, and snippets.

@CodingDoug
Created February 22, 2016 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodingDoug/43d17d44459805e51072 to your computer and use it in GitHub Desktop.
Save CodingDoug/43d17d44459805e51072 to your computer and use it in GitHub Desktop.
//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