ListView item项中包含某些可以抢焦点的控件导致无法获取焦点问题的解决方案
2014-10-24 15:53
357 查看
问题描述:如果你自定义ListView的item中包含能获得焦点的子控件(RadioGroup、Button、CheckBox、 DatePicker、ImageButton、Gridview、SeekBar、EditText、ToggleButton、 RatingBar等)的话,默认焦点会被交给这些子控件,而ListView的项能被选中的基础是它能获取焦点,所以项中的这些子控件的焦点必须为 false,这样ListView的项才能获取onItemLongClick事件与onItemClick事件。
解决办法:在布局文件中,在项的layout布局文件根元素中加入android:descendantFocusability="blocksDescendants"
Android api描述如下:
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.
该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
注意:ListView的项根元素layout的宽要设置成android:layout_width="fill_parent",否则可能会因为项内容太短,导致点击时获取不到焦点。
解决办法:在布局文件中,在项的layout布局文件根元素中加入android:descendantFocusability="blocksDescendants"
Android api描述如下:
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.
该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
注意:ListView的项根元素layout的宽要设置成android:layout_width="fill_parent",否则可能会因为项内容太短,导致点击时获取不到焦点。
相关文章推荐
- ListView条目中按钮或条目无法获取焦点问题
- android问题:控件id不能在R.java里自动创建,导致在Activity中无法获取控件
- ListView的item中含edittext时无法获取焦点的问题
- ListView里的item有强制获取焦点控件的问题
- ListView子控件获取焦点问题
- 解决listview的item中有Button、ImageButton等自动获取焦点的控件时,item获取不到焦点问题
- ListView子控件获取焦点问题
- DataForm中包含TabControl控件,FindNameInContent方法无法获取未选中TabItem里的控件问题
- android中listview包含edittext时,获取焦点问题
- android:descendantFocusability用法简析 解决listview中子控件导致item不能获取焦点
- 安卓listview嵌套gridview时,造成listview的item无法获取焦点的问题
- 关于ListView的item中包含Button或Checkbox时导致item无法点击问题
- ListView无法获取焦点之问题解决
- DataForm中包含TabControl控件,FindNameInContent方法无法获取未选中TabItem里的控件问题
- ASP.Net--ListView中无法获取TextBox值问题的解决方案
- 关于程序进入包含EditText控件的界面会自动获取焦点并弹出软键盘影响用户体验的问题
- Android疑惑篇------------含有button按钮的ListView中,列表项无法获取焦点的问题
- 通过android:descendantFocusability解决listview每一项或项中view无法获取焦点的问题
- ListView由于item项中包含某些可以…
- android关于Listview中定义了子控件,子控件和item同时获取焦点的问题