Home > OS >  I don't understand what is wrong
I don't understand what is wrong

Time:08-02

I am new in kotlin and i don't understand what is wrong, when i do a "CLICK" in the imageView (val_jugadorX.alpha = 0.5f) i can't unselect it again (val_jugadorX.alpha = 1f)

fun comprobarSeleccion(val_jugadorX: ImageView): Boolean{
            // si el boleano es false, no está seleccionado
            // si el boleano es verdadero, está seleccionado
            var devolver: Boolean = false

            if (val_jugadorX.alpha == 1f){
                val_jugadorX.setOnClickListener {
                    val_jugadorX.alpha = 0.5f
                    println("Ahora está seleccionado")
                    devolver = true
                }
            }else if(val_jugadorX.alpha== 0.5f) {
                val_jugadorX.setOnClickListener{
                    val_jugadorX.alpha = 1f
                    println("Ahora no está selccio")
                    devolver = false
                }
            }
            return devolver
        } 

CodePudding user response:

You're setting a click listener based on the ImageView's current alpha, and that click listener changes the alpha. But it doesn't call comprobarSeleccion again to set a new click listener based on its new alpha. You probably want to do something like this:

fun comprobarSeleccion(val_jugadorX: ImageView): Boolean{
            // si el boleano es false, no está seleccionado
            // si el boleano es verdadero, está seleccionado
            var devolver: Boolean = false

            if (val_jugadorX.alpha == 1f){
                val_jugadorX.setOnClickListener {
                    val_jugadorX.alpha = 0.5f
                    println("Ahora está seleccionado")
                    devolver = true
                    // now the alpha has changed, we need a new click listener
                    comprobarSeleccion(val_jugadorX)
                }
            } ...
        } 

Although something like this might be better (maybe set up during initialisation):

someImageView.setOnClickListener {
    with(someImageView) {
        val selected = alpha == 1f
        // update display
        alpha = if (selected) 0.5f else 1f
        // handle the selection change event
        setSelected(selected)
    }
}

That way you just set a click listener once, and it handles things depending on its current state. By having a separate setSelected function, you can tell another part of the app that this selection state has changed:

var devolver = false

fun setSelected(selected: Boolean) {
    println(if (selected) "Ahora está seleccionado" else "Ahora no está selccio")
    devolver = selected
    // any other stuff that needs to happen because of the selection change
}

That fixes your other problem - in your example, devolver is a local variable inside comprobarSeleccion. You set it to false, then return it (i.e. return false). If a click listener changes it later, nothing can access it. Next time you call comprobarSeleccion you get a new devolver set to false again.

By making it a top-level variable, your setSelected function can change it (or your click listener can if you want) and everything else can see its current value.

  • Related