views:

297

answers:

2

Hi,

I don't know exactly how to explain this problem, but I'll try. I have a ListView with several items. Each item has inside a TextView and two ImageView. I want the ImageView change when I click on them, and I want to open a context menu when I press for a long time into the ListView item.

For the ImageView, everything works properly. For the whole item, I can show the context menu after a long press, but my problem is that the ImageView changes as well when I am pressing the TextView, for example.

Somo pieces of my code:

ListView item:

     <TextView 
      android:id="@+id/title"
      android:textColor="@color/black"
      android:maxLines="2"
      android:textSize="14dip" 
            />
    <ImageView
        android:id="@+id/minus"
        android:src="@drawable/minusbutton"
        android:adjustViewBounds="true"
        android:gravity="center"
    />
    <ImageView 
        android:id="@+id/plus"
        android:src="@drawable/plusbutton"
        android:adjustViewBounds="true"
        android:gravity="center"
    />

Drawable to change the status of the plus button:

<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
<item android:state_enabled="false"
      android:drawable="@drawable/button_add_normal_disabled" />
<item android:state_enabled="true"
      android:state_pressed="true"
      android:drawable="@drawable/button_add_pressed" />
<item android:state_enabled="true"
      android:state_focused="true" 
      android:state_pressed="false" 
      android:drawable="@drawable/button_add_active" />
<item android:state_enabled="true"
      android:state_focused="false" 
      android:state_pressed="false"
      android:drawable="@drawable/button_add_normal" />

I hope you understand my problem. I think that all the children of a view are affected by an event in the parent, but I am not sure.

Do you have a solution? Thanks in advance

A: 

The easy and may be not very elegant solution - get rid of statefull drawable for you image view and handle presentation of this item in OnItemClickListener(). I would probably change state of the Item in the adapter, and then getView in the adapter should put right image depending on your state.

Alex Volovoy
Thanks Alex! I am going to use the "no elegant" version, because I tried it, and it works, but I would like to know if it is possible to implement it in a more elegant way :S
Antonio
A: 

I believe what's happening is the ListView is setting the state of the item's ViewGroup, and the children are duplicating the parent's state. So it's actually the row that is in state_pressed, and that gets inherited to the other views inside the row. There is an attribute, android:duplicateParentState="false", which I think should fix that.

Joe