Skip to content

Instantly share code, notes, and snippets.

@AaronC81
Last active July 23, 2016 07:58
Show Gist options
  • Save AaronC81/02a9f76730698a17ea3718bb4e2b2acd to your computer and use it in GitHub Desktop.
Save AaronC81/02a9f76730698a17ea3718bb4e2b2acd to your computer and use it in GitHub Desktop.
Anko Fragments v2
class FragmentView(ctx: Context) : RelativeLayout(ctx) {
var childFragment: Fragment? = null
get() = field
set(value) {
field = value
if (field == null) { return }
(context as Activity).fragmentManager.beginTransaction().add(id, field).commit()
}
init {
this.id = View.generateViewId()
}
}
inline fun ViewManager.fragmentView(theme: Int = 0) = fragmentView(theme) {}
inline fun ViewManager.fragmentView(theme: Int = 0, init: FragmentView.() -> Unit) = ankoView({ FragmentView(it) }, theme, init)
@AaronC81
Copy link
Author

Requires API 17 or above due to View.generateViewId()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment