Anko: lparams() for Layouts/_CollapsingToolbarLayout returns FrameLayout.LayoutParams

Created on 26 Nov 2016  路  4Comments  路  Source: Kotlin/anko

fun <T: View> T.lparams(
            c: android.content.Context?,
            attrs: android.util.AttributeSet?,
            init: android.widget.FrameLayout.LayoutParams.() -> Unit = defaultInit
    ): T {
        val layoutParams = android.widget.FrameLayout.LayoutParams(c!!, attrs!!)
        layoutParams.init()
        [email protected] = layoutParams
        return this
    }

etc.

But CollapsibleToolbarLayout has its own LayoutParams with additional features. Is it a bug or does it have some particular reason?

Thanks

bug fixed

Most helpful comment

Yes, it's a bug. Just put the following code anywhere:

fun <T : android.view.View> T.collapsingToolbarlparams(
        width: kotlin.Int = wrapContent, height: kotlin.Int = wrapContent,
        init: android.support.design.widget.CollapsingToolbarLayout.LayoutParams.() -> kotlin.Unit = {}): T {
    val layoutParams = android.support.design.widget.CollapsingToolbarLayout.LayoutParams(width, height)
    layoutParams.init()
    this.layoutParams = layoutParams
    return this
}

And then

collapsingToolbarLayout {
                    view().collapsingToolbarlparams {
                        collapseMode = CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_PARALLAX
                    }
                }

All 4 comments

Yes, it's a bug. Just put the following code anywhere:

fun <T : android.view.View> T.collapsingToolbarlparams(
        width: kotlin.Int = wrapContent, height: kotlin.Int = wrapContent,
        init: android.support.design.widget.CollapsingToolbarLayout.LayoutParams.() -> kotlin.Unit = {}): T {
    val layoutParams = android.support.design.widget.CollapsingToolbarLayout.LayoutParams(width, height)
    layoutParams.init()
    this.layoutParams = layoutParams
    return this
}

And then

collapsingToolbarLayout {
                    view().collapsingToolbarlparams {
                        collapseMode = CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_PARALLAX
                    }
                }

hi, i've just encountered the very same problem. nesting one layout (linear) into another (relative) leads to an ambiguous lparams reference:

lparams_ambiguity

there is no way i know to tell the compiler whichlparams i mean, so it takes always the linear layout one! even something like [email protected] does not work...

my current workaround is to do it the hard way and dismiss the DSL approach:

relativeLayout {
  linearLayout {
    layoutParams = RelativeLayout.LayoutParams(wrapContent, wrapContent).apply {
      alignParentRight() // which is only available for RelativeLayout params
    }
  }
}

the long way as suggested above, by reimplementing lparams but with another name to avoid the name clash which leads to this ambiguity would be:

private val defaultInit: Any.() -> Unit = {}
fun <T: View> T.lparams_rl(
        width: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        height: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        init: RelativeLayout.LayoutParams.() -> Unit = defaultInit
): T {
    val layoutParams = RelativeLayout.LayoutParams(width, height)
    layoutParams.init()
    this@lparams_rl.layoutParams = layoutParams
    return this
}

so now we can tell the compiler with layout params we want:
lparams_extended

can anyone fix this bug
just like this, so simple

```open class _CollapsingToolbarLayout(ctx: Context): CollapsingToolbarLayout(ctx) {
fun T.lparams(
c: Context?,
attrs: AttributeSet?,
init: CollapsingToolbarLayout.LayoutParams.() -> Unit = org.jetbrains.anko.design.defaultInit
): T {
val layoutParams = CollapsingToolbarLayout.LayoutParams(c!!, attrs!!)
layoutParams.init()
[email protected] = layoutParams
return this
}

fun <T: View> T.lparams(
        width: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        height: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        init: CollapsingToolbarLayout.LayoutParams.() -> Unit = org.jetbrains.anko.design.defaultInit
): T {
    val layoutParams = CollapsingToolbarLayout.LayoutParams(width, height)
    layoutParams.init()
    [email protected] = layoutParams
    return this
}

fun <T: View> T.lparams(
        width: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        height: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
        gravity: Int,
        init: CollapsingToolbarLayout.LayoutParams.() -> Unit = org.jetbrains.anko.design.defaultInit
): T {
    val layoutParams = CollapsingToolbarLayout.LayoutParams(width, height, gravity)
    layoutParams.init()
    [email protected] = layoutParams
    return this
}

fun <T: View> T.lparams(
        source: ViewGroup.LayoutParams?,
        init: CollapsingToolbarLayout.LayoutParams.() -> Unit = org.jetbrains.anko.design.defaultInit
): T {
    val layoutParams = CollapsingToolbarLayout.LayoutParams(source!!)
    layoutParams.init()
    [email protected] = layoutParams
    return this
}

fun <T: View> T.lparams(
        source: ViewGroup.MarginLayoutParams?,
        init: CollapsingToolbarLayout.LayoutParams.() -> Unit = org.jetbrains.anko.design.defaultInit
): T {
    val layoutParams = CollapsingToolbarLayout.LayoutParams(source!!)
    layoutParams.init()
    [email protected] = layoutParams
    return this
}

fun <T: View> T.lparams(
        source: CollapsingToolbarLayout.LayoutParams?,
        init: CollapsingToolbarLayout.LayoutParams.() -> Unit = org.jetbrains.anko.design.defaultInit
): T {
    val layoutParams = CollapsingToolbarLayout.LayoutParams(source!!)
    layoutParams.init()
    [email protected] = layoutParams
    return this
}

}
```

we need someone to do this job

Fixed with 1b5653cdb9648ebc316aeeb13179ebddfd91d4fa

Was this page helpful?
0 / 5 - 0 ratings