Listview and Imagebutton selector does not work

Asked

Viewed 90 times

1

Both the button and Listview do not change when clicked, but the settings when the item is not pressed work.

Listview:

<ListView
        android:id="@+id/listaListas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="#FFECECEC"
        android:dividerHeight="2sp"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/novalista"
        android:background="@drawable/layout_item_listview">
    </ListView>

layout_item_listview:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:state_selected="true"
        android:drawable="@drawable/layout_item_selecionado" />
    <item android:state_pressed="false" android:state_selected="false"
        android:drawable="@drawable/layout_item_normal" />
</selector>

layout_item_selected:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/item_selecionado"/>
</shape>

layout_item_normal:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/vermelho"/>
</shape>

Boot:

<ImageButton
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:background="@color/amarelo"
            android:src="@drawable/img_estrelabotao"
            android:id="@+id/btnfavorito"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:visibility="visible"
            style="@style/estiloBotaoFavorito"/>

styleBotaoFavorite:

<style name="estiloBotaoFavorito">
        <item name="android:background">@drawable/layout_botao_favorito</item>
        <item name="android:textColor">@color/icons</item>
        <item name="android:textSize">15dp</item>
    </style>

layout_favorite boot:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/layout_botao_favorito_pressionado"/>
    <item android:state_pressed="false" android:drawable="@drawable/layout_botao_favorito_normal"/>
</selector>

layout_botao_favorito_pressionado:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid
        android:drawable="@drawable/nine_patch_retangulo"
        android:color="@color/amarelo_escuro"
        android:elevation="8dp"/>
</shape>

layout_botao_favorito_normal:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid
        android:drawable="@drawable/nine_patch_retangulo"
        android:color="@color/amarelo"
        android:elevation="8dp"/>
</shape>

Another question I have is whether inside the button Shape I can set Elevation and drawable? How did I fail I left so

1 answer

1


The order in which states are declared has importance in the way they are applied, which is not always obvious.

The best way to avoid these situations is to declare as few states as possible and do so in separate items.

Change the selectors like this:

layout_item_listview:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/layout_item_selecionado" />
    <item android:state_selected="true"
        android:drawable="@drawable/layout_item_selecionado" />
    <item android:state_activated="true" 
        android:drawable="@drawable/layout_item_selecionado"/>
    <item android:drawable="@drawable/layout_item_normal" />
</selector>

To enable the selection it is necessary to add android:state_activated="true" and to be declared in Listview, the attribute android:choiceMode with a different way of none.

Note: This selector must be assigned to the background of layout the list item and not the Listview.

layout_favorite boot:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"
        android:drawable="@drawable/layout_botao_favorito_pressionado"/>
    <item android:drawable="@drawable/layout_botao_favorito_normal"/>
</selector>

The last <item>, the one that has not been defined will be applied by default(when none of the above).

Regarding the other doubt, the documentation only indicates the attribute android:color can be used in the element <solid>.

  • I did what you said and put the choiceMode as singleChoice and continued not working

  • I edited the answer, the state was missing state_activated. Pay attention to the note I added.

  • 1

    It worked! Buttons were not working because besides the style I had set a background. I took the background and it worked fine! Thanks :)

  • I have the same problem, my code is the way you spoke ramaral, but only when I click on the image button it does something, now when I click on the listview item it does not apparently select it or claim the function onClick (if I take the button works normally)my choiceMode is multipleChoice.

  • @Joãocarlos without seeing the code becomes difficult to know the reason. See if you are doing everything described in the answer. If not solved, open a new question.

  • I asked a question of a little glance at me.https://answall.com/questions/237218/ao-addir-imagebutton-selector-de-listview-workstation? noredirect=1#comment485869_237218

  • I saw her shortly after I commented. I see nothing wrong with the problem that indicates.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.