Skip to content

Commit

Permalink
no exception on inflate
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jun 26, 2020
1 parent 1dd69e0 commit a67b3fc
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ val View.activity: AppCompatActivity?
internal fun KClass<out ViewDataBinding>.inflate(
component: Component<*, *>,
attachToRoot: Boolean
) = (component.container as? ViewGroup)?.let {
) = (component.container as? ViewGroup).let {
(java.getMethod(
"inflate",
LayoutInflater::class.java,
ViewGroup::class.java,
Boolean::class.javaPrimitiveType
).invoke(null, LayoutInflater.from(it.context), it, attachToRoot) as ViewDataBinding).apply {
).invoke(
null,
LayoutInflater.from((component as? Fragment)?.context ?: (component as? View)?.context),
it,
attachToRoot
) as ViewDataBinding).apply {
this.lifecycleOwner = component.boundLifecycleOwner
setComponent(component)
setViewModel(component.viewModel)
}
} ?: throw UnsupportedOperationException("Component must be ViewGroup")
}

internal fun ViewDataBinding.setViewModel(viewModel: ComponentViewModel) {
this::class.java.getMethod("setViewModel", viewModel::class.java).invoke(this, viewModel)
Expand Down

0 comments on commit a67b3fc

Please sign in to comment.