BrotherV/Floating-ArcMenu

OnClickListener problem (Kotlin)

Closed this issue · 1 comments

When I press the button the menu opens and when I press an item for the first time, the menu closes. I press the button again, open the menu and now, when the item is pressed, the corresponding OnClick is executed. That happens with all the menu items. They work well from the second press, never on the first.

This is my code:

`val itemsIconos = intArrayOf(R.drawable.color, R.drawable.align, R.drawable.formato, R.drawable.posicion, R.drawable.escala)
val itemsTexto = arrayOf("Color", "Alineación", "Formato", "Posición", "Escala")

arcMenu.showTooltip(true)
arcMenu.setToolTipBackColor(ContextCompat.getColor(this, R.color.primaryLightColor))
arcMenu.setToolTipCorner(6f)
arcMenu.setToolTipPadding(2f)
arcMenu.setToolTipTextSize(16)
arcMenu.setToolTipTextColor(ContextCompat.getColor(this, R.color.primaryTextColor))
arcMenu.setAnim(300, 300, ArcMenu.ANIM_MIDDLE_TO_RIGHT, ArcMenu.ANIM_MIDDLE_TO_RIGHT, ArcMenu.ANIM_INTERPOLATOR_ACCELERATE_DECLERATE, ArcMenu.ANIM_INTERPOLATOR_ACCELERATE_DECLERATE)

val itemCount = itemsIconos.size
for (i in 0 until itemCount) {
    val item = FloatingActionButton(this)
    item.setSize(FloatingActionButton.SIZE_MINI)
    item.setIcon(itemsIconos[i])
    item.setBackgroundColor(ContextCompat.getColor(this, R.color.primaryDarkColor))
    arcMenu.setChildSize(item.intrinsicHeight)

    arcMenu.addItem(item, itemsTexto[i], {
        //añadir funciones listener a los botones
        when (i) {
            0 -> it.setOnClickListener {
                fl_ac_mo.contentRes = R.layout.mn_flo_color
                fl_ac_mo.open()
            }
            1 -> it.setOnClickListener {
                fl_ac_mo.contentRes = R.layout.mn_flo_align
                fl_ac_mo.open()
            }
            2 -> it.setOnClickListener {
                fl_ac_mo.contentRes = R.layout.mn_flo_formato
                fl_ac_mo.open()
            }
            3 -> it.setOnClickListener {
                fl_ac_mo.contentRes = R.layout.mn_flo_posicion
                fl_ac_mo.open()
            }
            4 -> it.setOnClickListener {
                fl_ac_mo.contentRes = R.layout.mn_flo_escala
                fl_ac_mo.open()
            }
        }
    })
}`

Ok. I got it.

Listener into listener not good idea.

This is the solution:

arcMenu.addItem(item, itemsTexto[i], View.OnClickListener() { //añadir funciones listener a los botones when (i) { 0 -> fl_ac_mo.contentRes = R.layout.mn_flo_color 1 -> fl_ac_mo.contentRes = R.layout.mn_flo_align 2 -> fl_ac_mo.contentRes = R.layout.mn_flo_formato 3 -> fl_ac_mo.contentRes = R.layout.mn_flo_posicion 4 -> fl_ac_mo.contentRes = R.layout.mn_flo_escala } fl_ac_mo.open() })

It works fine.

Thanks.