Skip to content

Commit a67b3fc

Browse files
Lunkov_A@utkonos.ruLunkov_A@utkonos.ru
authored andcommitted
no exception on inflate
1 parent 1dd69e0 commit a67b3fc

File tree

1 file changed

+8
-3
lines changed
  • ui-generator-base/src/main/java/ru/impression/ui_generator_base

1 file changed

+8
-3
lines changed

ui-generator-base/src/main/java/ru/impression/ui_generator_base/Ext.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,23 @@ val View.activity: AppCompatActivity?
3131
internal fun KClass<out ViewDataBinding>.inflate(
3232
component: Component<*, *>,
3333
attachToRoot: Boolean
34-
) = (component.container as? ViewGroup)?.let {
34+
) = (component.container as? ViewGroup).let {
3535
(java.getMethod(
3636
"inflate",
3737
LayoutInflater::class.java,
3838
ViewGroup::class.java,
3939
Boolean::class.javaPrimitiveType
40-
).invoke(null, LayoutInflater.from(it.context), it, attachToRoot) as ViewDataBinding).apply {
40+
).invoke(
41+
null,
42+
LayoutInflater.from((component as? Fragment)?.context ?: (component as? View)?.context),
43+
it,
44+
attachToRoot
45+
) as ViewDataBinding).apply {
4146
this.lifecycleOwner = component.boundLifecycleOwner
4247
setComponent(component)
4348
setViewModel(component.viewModel)
4449
}
45-
} ?: throw UnsupportedOperationException("Component must be ViewGroup")
50+
}
4651

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

0 commit comments

Comments
 (0)