单击项目中的元素的ListView项的变化状态? [英] Click in a ListView item changes status of elements inside the item?
问题描述
我不知道究竟该怎么解释这个问题,但我会尽力。我有几个项目一个ListView。每个项目都有一个TextView和两个ImageView的内部。我想ImageView的变化,当我点击它们,我要打开上下文菜单,当我preSS很长一段时间进入ListView项。
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.
有关的ImageView的,一切工作正常。对于整个项目,我可以显示一个长preSS后的上下文菜单,但我的问题是,当我pressing TextView的,例如ImageView的变化也是如此。
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.
我的code的索莫件:
Somo pieces of my code:
ListView项:
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
推荐答案
解决这个问题的最简单方法是继承ViewGroup中,并覆盖dispatchSet $ P $ pssed。
The easiest way to solve this issue is to inherit viewgroup and override dispatchSetPressed.
下面是为例
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);
}
}
}
}
使用这种方法的缺点是,你将不得不duplicateparentstate设置为true,你想不应该有这样的行为,每一个项目和子项目。
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.
这篇关于单击项目中的元素的ListView项的变化状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!