ListView中的Button事件及ListView Item焦点的冲突处理

本文介绍了Android中关于ViewGroup与其子视图之间的焦点控制机制,并详细解释了`android:descendantFocusability`属性的使用方法。文章还提供了具体的解决方案,帮助开发者解决ListView中Button等控件的点击事件响应问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android:descendantFocusability
定义父子控件之间获取焦点的关系   

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

ConstantValueDescription
beforeDescendants0The ViewGroup will get focus before any of its descendants.  父控件优先获取
afterDescendants1The ViewGroup will get focus only if none of its descendants want it. 子控件优先获取
blocksDescendants2The ViewGroup will block its descendants from receiving focus. 屏蔽子控件获取控件

This corresponds to the global attribute resource symbol descendantFocusability.


       在ListView有Button等需要被点击的控件,如果不对相关属性进行设置,ListView的item点击事件将不能响应。ListView中的Button等控件需要每个Item都绑定一次,不然触发的时候会得不到想要的结果。

 解决的办法,在ListView的Item的xml文件中添加如下属性:

                1)添加android:descendantFocusability="blocksDescendants"

                2)Button添加属性android:focusable="false"和android:clickable="true"

这样设置后ListView和Item中的Button都具有Click的功能。

点击Button后Item变色 以此类推 这个还在研究中....


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值