如果stu_item.xml里面包括button或者checkbox等控件,默认情况下list的item会失去焦点,导致无法响应item的事件,最常用的解决办法是在stu_item.xml的布局文件中设置descendantFocusability属性。
该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点 afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点 blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:descendantFocusability="blocksDescendants"><!--添加这个属性--> <ImageView android:id="@+id/img_head" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/dog2" android:scaleType="centerCrop" android:layout_marginRight="5dp"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:id="@+id/tv_name"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:id="@+id/tv_age"/> <Button android:id="@+id/btn_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="删除" android:textColor="#ffffff" android:background="#ff0000" /> </LinearLayout>
当前题目:Android中ListView的item点击没有反应的解决方法-创新互联
网页链接:https://www.cdcxhl.com/article42/dohohc.html
成都网站建设公司_创新互联,为您提供网站策划、软件开发、用户体验、手机网站建设、营销型网站建设、品牌网站设计
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联