Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

lparams() for Layouts/_CollapsingToolbarLayout returns FrameLayout.LayoutParams #275

Closed
GeorgiyShur opened this issue Nov 26, 2016 · 4 comments

Comments

@GeorgiyShur
Copy link

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()
        this@lparams.layoutParams = 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

@skrugly
Copy link

skrugly commented Nov 26, 2016

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
                    }
                }

@christophpickl
Copy link

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 which lparams i mean, so it takes always the linear layout one! even something like this@relativeLayout.lparams 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

@not3
Copy link

not3 commented Mar 2, 2017

can anyone fix this bug
just like this, so simple

    fun <T: View> T.lparams(
            c: Context?,
            attrs: AttributeSet?,
            init: CollapsingToolbarLayout.LayoutParams.() -> Unit = org.jetbrains.anko.design.defaultInit
    ): T {
        val layoutParams = CollapsingToolbarLayout.LayoutParams(c!!, attrs!!)
        layoutParams.init()
        this@lparams.layoutParams = 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()
        this@lparams.layoutParams = 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()
        this@lparams.layoutParams = 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()
        this@lparams.layoutParams = 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()
        this@lparams.layoutParams = 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()
        this@lparams.layoutParams = layoutParams
        return this
    }

}

we need someone to do this job

@yanex yanex added the fixed label May 3, 2017
@4u7 4u7 added the bug label Nov 17, 2017
@4u7
Copy link
Contributor

4u7 commented Nov 17, 2017

Fixed with 1b5653c

@4u7 4u7 closed this as completed Nov 17, 2017
@4u7 4u7 added this to the Anko 0.10.3 milestone Nov 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants