If we take a look at Anko sources we can see that frameLayout
actualy returns an instance of _FrameLayout
class, where these lparams
functions are defined. In my understanding this is needed, so these lparams
functions are only available in the layout building code.
In Anko's Layouts.kt
file there are these _<ViewGroup>
classes for every supported ViewGroup
.
So the straightforward way to support a custom view group is to create a _<ViewGroup>
class with lparams
methods implementation. The problem is that this _<ViewGroup>
class often contains much more code than my <ViewGroup>
itself!
And if I want create many custom view groups, adding Anko support for them becomes a big pain with this approach. Let's say I have MyFrameLayout1
, MyFrameLayout2
, MyFrameLayout3
classes. They are basically FrameLayout
's so I want the same layout parameters to be used with them. But I have to create _FrameLayout1
, _FrameLayout2
, _FrameLaoyt3
classes which are just copy/paste of Anko's _FrameLayout
.
So I slightly improved this approach. I create an interface _FrameLayout
:
interface _FrameLayout {
// copy/paste from Anko's _FrameLayout
}
and now to support any custom FrameLayout
subclass I just have to:
class _MyFrameLayout(ctx: Context) : MyFrameLayout(ctx), _FrameLayout
fun ViewManager.myFrameLayout(init: _MyFrameLayout.() -> Unit = {})= ankoView({ _MyFrameLayout(it) }, init)
This reduces copy/paste a lot, when creating many custom view groups.