android listview和button,ImageButton等有事件的控件的总结
1 2 3 4 | public ImageButton(Context context, AttributeSet attrs, int defStyle) { super (context, attrs, defStyle); setFocusable( true ); } |
在listview中(或者ExpandableListview),item的view会被进行特殊的处理,通过convertview可以减少解析xml文件,提高效率。但是如果你自己解析一次,然后用变量保存,那么只有最后一个view才能正常显示,说明了每一个item的view都是不同的实例,这样就注定了每一个view事实上是可以添加按钮之类的单独响应事件的。
有这么几种方法避免Button或者ImageButton抢夺整个item的焦点
- Button设置focusable=false,ImageButton要通过代码设置ib.setFocusable(false),这是因为源码的bug
1 2 3 4 | public ImageButton(Context context, AttributeSet attrs, int defStyle) { super (context, attrs, defStyle); setFocusable( true ); } |
2. 在item的layout的根viewgroup中设置android:descendantFocusability="blocksDescendants",这个属性也会使其他的view能响应焦点
但是在实际开发过程中,我发现如果是ExpandableLisview的BaseExpandableListAdapter中的isChildSelectable方法覆写之后得返回true,否则也不能响应ItemClick
1 2 3 4 | @Override public boolean isChildSelectable( int groupPosition, int childPosition) { return true ; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?