Click in a ListView item changes status of elements inside the item?
Asked Answered
B

3

3

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">
<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

Budweis answered 9/4, 2010 at 13:15 Comment(1)
Have a look at my solution, it's simple.Parch
C
6

The easiest way to solve this issue is to inherit viewgroup and override dispatchSetPressed.

Here is an exemple

public class DuplicateParentStateAwareLinearLayout extends LinearLayout {

    public DuplicateParentStateAwareLinearLayout(Context context) {
        super(context);
    }

    public DuplicateParentStateAwareLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public DuplicateParentStateAwareLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /*
     * By default ViewGroup call setPressed on each child view, this take into account duplicateparentstate parameter
     */
     @Override
     protected void  dispatchSetPressed(boolean pressed) {
          for (int i = 0; i < getChildCount(); i++) {
             View child = getChildAt(i);
             if (child.isDuplicateParentStateEnabled()){
                 getChildAt(i).setPressed(pressed);
             }
         }
      }
}

The catch using this method is that you will have to set duplicateparentstate to true for each items you want to and subitems that shouldn't have this behavior.

Cephalad answered 4/9, 2012 at 17:54 Comment(0)
C
3

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.

Consubstantiation answered 16/6, 2010 at 19:28 Comment(1)
Thanks for the attribute. It really looked great to me, unfortunately it didn't work…Parch
M
0

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.

Merlon answered 9/4, 2010 at 14:0 Comment(2)
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 :SBudweis
#6742408Thales

© 2022 - 2024 — McMap. All rights reserved.