Android: problem with OnClick in Floating-ArcMenu

0

I'm doing an app with Android Studio and I use a component called Floating-ArcMenu ( link ) that is great. The problem is the following: 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, the menu opens 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.

I include capture of the displayed menu:

I include code of the creation of the menu and its items:

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

How do I resolve for the OnClick of the items to work from the first opening?

Thanks

    
asked by Fer Nando 02.06.2018 в 09:53
source

1 answer

0

Solved.

A listener inside a listener is not a good idea.

Here 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()
        })
    
answered by 02.06.2018 в 21:03