问题
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
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/2607698/click-in-a-listview-item-changes-status-of-elements-inside-the-item