gpt4 book ai didi

android - 每个 ListView 行中的按钮按下状态问题

转载 作者:行者123 更新时间:2023-11-29 16:11:51 25 4
gpt4 key购买 nike

我在 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:clickable="true" 设置为父 View 。

这样它会拦截触摸事件, subview 不会高亮。

在您的具体案例中:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainListLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">

关于android - 每个 ListView 行中的按钮按下状态问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12503207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com