Android中ListView的item点击没有反应的解决方法

发布时间:2020-09-01 08:51:16 作者:笑对空
来源:脚本之家 阅读:208

如果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点击没有反应的解决方法

总结

以上所述是小编给大家介绍的Android中ListView的item点击没有反应,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

推荐阅读:
  1. android listview中item通过viewpager实现
  2. Android中ListView存在多个item样式怎么处理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android listview item

上一篇:深入浅出OOP(三): 多态和继承(动态绑定/运行时多态)

下一篇:详解ES6 export default 和 import语句中的解构赋值

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》