【发布时间】:2012-09-19 21:31:57
【问题描述】:
我在 ListView 行中按下按钮时遇到问题。每个 Button 的背景属性引用一个 XML 选择器文件;以便在按下按钮时选择不同的图像。
我能够从 OnClickListener 获取新闻事件,但是状态选择器中断并且没有向 android:state_pressed="true" 和 android:state_focused="false" 注册新闻。
如果我从按钮的父/根布局 XML 中删除 android:descendantFocusability="blocksDescendants";那么按下状态将起作用,但无论您触摸 ListView 行的哪个位置都会触发,这很烦人。
我的问题:无法管理与内部按钮分开的行的按下/默认状态。它们相互干扰。
代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainListLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
..........
行项目按钮再往下:
<Button
android:id="@+id/deleteButton"
android:background="@drawable/del_button_selector"
.....
.....
/>
..........
还有drawable/del_button_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/deleteico" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/deletepressed" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/deleteico" android:state_focused="false" android:state_pressed="true" />
在最后一个选择器行中更改可绘制背景不起作用。它将显示按下的图像,但无论您单击该行的哪个位置,远离按钮。
我可以更改按钮单击事件的背景,但我需要在按钮释放时切换回默认背景,这很难捕获(?)。如果我可以在侦听器中捕获按下/释放事件,那么这仅适用于按钮。
任何帮助表示赞赏!谢谢!
【问题讨论】:
标签: android listview button android-listview onclick