ListView的OnItemClick无法响应以及子Item控件的事件处理。
2016-03-08 14:56
447 查看
如果你的listview item中有Button或Checkable的子类控件的话,那么默认focus是交给了子控件的,而ListView的Item能被选中的基础是它能获得focus ,也就是说,我们可以通过将ListView的Item中包含的所有控件的focusable属性设为false,这样的话,ListView的Item就可以自动获得Focus了,也就可以被选中了。另外,对于属性 android:textIsSelectable也要设置成"false"
我们可以通过对Item Layout的根控件设置其 android:descendantFocusability="blocksDescendants"即可。这样Item
Layout就屏蔽了所有子控件获取Focus的权限,而不再需要多Item Layout中的每一个控件重新设置focusable属性了。
另外,关于ListView的Item与Item中子控件同时可响应事件的方法
ListView的setOnItemClickListener事件和ListView中Item中包含的子控件(比如button)的click事件共存的解决办法:
在ListView的item的xml配置文件的根节点添加属性
android:descendantFocusability="blocksDescendants",
并且,在要添加事件的子控件(如button)的属性里添加android:focusable="false"
另外,注意:有时现成的几个adapter满足不了要求,此时就需要继承自BaseAdapter。
下面我是程序中的部分代码,该布局文件时listview中的item的布局,
listview_listitem_layout.xml 代码如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
style="@style/ListItem">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<ImageView
android:layout_width="45dip"
android:layout_height="45dip"
android:layout_gravity="center"
android:layout_alignParentLeft="true"
android:background="@drawable/imageview_background"
android:scaleType="fitXY" />
<Button
android:layout_width="@dimen/btn_attention_width"
android:layout_height="@dimen/btn_attention_height"
android:layout_alignParentRight="true"
android:background="@drawable/button_selector_gradient"
android:text="关注"
android:focusable="false"/>
</RelativeLayout>
我们可以通过对Item Layout的根控件设置其 android:descendantFocusability="blocksDescendants"即可。这样Item
Layout就屏蔽了所有子控件获取Focus的权限,而不再需要多Item Layout中的每一个控件重新设置focusable属性了。
另外,关于ListView的Item与Item中子控件同时可响应事件的方法
ListView的setOnItemClickListener事件和ListView中Item中包含的子控件(比如button)的click事件共存的解决办法:
在ListView的item的xml配置文件的根节点添加属性
android:descendantFocusability="blocksDescendants",
并且,在要添加事件的子控件(如button)的属性里添加android:focusable="false"
另外,注意:有时现成的几个adapter满足不了要求,此时就需要继承自BaseAdapter。
下面我是程序中的部分代码,该布局文件时listview中的item的布局,
listview_listitem_layout.xml 代码如下:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
style="@style/ListItem">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<ImageView
android:layout_width="45dip"
android:layout_height="45dip"
android:layout_gravity="center"
android:layout_alignParentLeft="true"
android:background="@drawable/imageview_background"
android:scaleType="fitXY" />
<Button
android:layout_width="@dimen/btn_attention_width"
android:layout_height="@dimen/btn_attention_height"
android:layout_alignParentRight="true"
android:background="@drawable/button_selector_gradient"
android:text="关注"
android:focusable="false"/>
</RelativeLayout>
相关文章推荐
- ROS - “hello world!”
- BZOJ 3110 [Zjoi2013]K大数查询
- jedis调用redis之String数据Key-Value
- 出租车GPS轨迹数据和手机数据的研究价值
- 离开微软这两年
- TextView 动态设置图片
- 动画入门
- SK-Learn使用NMF(非负矩阵分解)和LDA(隐含狄利克雷分布)进行话题抽取
- ubuntu下使用libsvm
- 当前时间
- Mysql INNER,LEFT ,RIGHT join的使用
- java发送http的get、post请求
- Android开发实现调用相册图片并裁剪上传功能
- js比较日期字符串
- Leetcode ☞ 203. Remove Linked List Elements ☆ 【dummy 方法二重点掌握】 【方法三双重指针】
- Last_Error: Slave SQL thread retried transaction 10 time(s) in vain, giving up. Consider raising the
- [小技巧] 如何自动将阿拉伯数字转换为中文大写数字
- android中跨进程通讯的4种方式
- media query ie8- 兼容实现总结
- Effiective C++ (一)