欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-16 16:35:22
如果stu_item.xml里面包括button或者checkbox等控件,默认情况下list的item会失去焦点,导致无法响应item的事件,最常用的解决办法是在stu_...

如果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点击没有反应,希望对大家有所帮助