单击 ListView 项目会更改项目内元素的状态吗?
我不知道如何解释这个问题,但我会尝试。我有一个包含多个项目的 ListView。每个项目内部都有一个 TextView 和两个 ImageView。我希望当我单击它们时 ImageView 会发生变化,并且当我长时间按下 ListView 项时我想打开一个上下文菜单。
对于 ImageView,一切正常。对于整个项目,我可以在长按后显示上下文菜单,但我的问题是,例如,当我按下 TextView 时,ImageView 也会发生变化。
我的代码片段:
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 来更改加号按钮的状态:
<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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决这个问题最简单的方法就是继承viewgroup并重写dispatchSetPressed。
下面是一个示例
使用此方法的问题是,您必须将您想要的每个项目和不应该具有此行为的子项目的重复父状态设置为 true。
The easiest way to solve this issue is to inherit viewgroup and override dispatchSetPressed.
Here is an exemple
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 正在设置项目的 ViewGroup 的状态,并且子级正在复制父级的状态。所以它实际上是 state_pressed 中的行,并且继承到该行内的其他视图。有一个属性
android:duplicateParentState="false"
,我认为应该可以解决这个问题。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.简单但可能不是很优雅的解决方案 - 摆脱图像视图的有状态可绘制并在 OnItemClickListener() 中处理此项的呈现。我可能会更改适配器中项目的状态,然后适配器中的 getView 应根据您的状态放置正确的图像。
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.