Android 中ListView点击Item无响应问题的解决办法

如果listitem里面包括button或者checkbox等控件,默认情况下listitem会失去焦点,导致无法响应item的事件,最常用的解决办法是在listitem的布局文件中设置descendantFocusability属性。

item的布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:descendantFocusability="blocksDescendants"><!--添加这个属性--> <CheckBox android:id="@+id/history_item_checkbt" android:layout_height="30dp" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:checked="false" > </CheckBox> <ImageView android:id="@+id/history_item_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/history_item_checkbt" android:background="@drawable/item_icon"> </ImageView> <Button android:id="@+id/history_item_edit_bt" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="编辑" android:textColor="#ffffff" android:textSize="14sp" android:background="@drawable/button_bg"> </Button> <TextView android:id="@+id/history_item_time_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:textColor="#565C5D" android:textSize="14sp" android:text="10-01 10:20" android:layout_marginRight="5dp" android:layout_toLeftOf="@id/history_item_edit_bt"> </TextView> <TextView android:id="@+id/history_item_title_tv" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_centerVertical="true" android:textColor="#565C5D" android:textSize="14sp" android:text="xxxxxxxxXXXXXXXXXXXXXXXX" android:ellipsize="end" android:maxLines="1" android:layout_toRightOf="@id/history_item_image" android:layout_toLeftOf="@id/history_item_time_tv" android:layout_marginLeft="3dp"> </TextView> </RelativeLayout>

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会覆盖子类控件而直接获得焦点

我们使用的是第三个。

时间: 2024-08-01 18:20:31

Android 中ListView点击Item无响应问题的解决办法的相关文章

onitemclick-关于android中listview点击item没有反应

问题描述 关于android中listview点击item没有反应 public class MapActivity extends BasePagerActivity { private MapView mMapView = null; private BaiduMap mBaiduMap; private ListView map_menu_lv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO A

Android中Listview点击item不变颜色及设置listselector 无效的解决方案_Android

这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listselector,有一个默认的颜色,同理如果点击没颜色变化我们怎么设置listselector也不会变颜色的. 但是在我们的开发过程中,我们可能会碰到这样的问题listview点击不变颜色,总结了一下大概有这几种原因: 1.item的layout设置background颜色值,去掉背景颜色即可 2.listview中listselector属性的效果被覆盖了,比如列表的Item为一个占满单元格的I

Android中Listview点击item不变颜色及设置listselector 无效的解决方案

这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listselector,有一个默认的颜色,同理如果点击没颜色变化我们怎么设置listselector也不会变颜色的. 但是在我们的开发过程中,我们可能会碰到这样的问题listview点击不变颜色,总结了一下大概有这几种原因: 1.item的layout设置background颜色值,去掉背景颜色即可 2.listview中listselector属性的效果被覆盖了,比如列表的Item为一个占满单元格的I

Android关于listview点击item中某一个按钮把这个item置顶的问题

问题描述 Android关于listview点击item中某一个按钮把这个item置顶的问题 大神告知我如何点击listview中item的某一个按钮·把相应的item进行置顶呢?或者说不用listview用动态加载如何实现呢? 解决方案 置顶,其实就是把你点击的这个item放到数据源列表的第一项,然后notify一下 解决方案二: 比如你的listview对应的数据列表为items,在getview时,可以在每个试图中保留位置postion,当点击时,取出postion,然后取出items中p

android-安卓中按钮点击事件无响应

问题描述 安卓中按钮点击事件无响应 我在xml中写了一个按钮,对这个按钮设置了setOnClickListener事件,运行时点击按钮没反应,小白一枚,特来请教 解决方案 Button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent mIntent = new Intent(); mIntent.setClass(MainActivity.this, R

Android中RecyclerView点击Item设置事件_Android

在上一篇Android RecylerView入门教程中提到,RecyclerView不再负责Item视图的布局及显示,所以RecyclerView也没有为Item开放OnItemClick等点击事件,这就需要开发者自己实现.博客最下面有Demo程序运行动画. 奉上Demo的Github链接. 在调研过程中,发现有同学修改RecyclerView源码来实现Item的点击监听,但认为这不是一个优雅的解决方案,最终决定在RecyclerView.ViewHolder上做文章. 思路是:因为ViewH

Android中RecyclerView点击Item设置事件

在上一篇Android RecylerView入门教程中提到,RecyclerView不再负责Item视图的布局及显示,所以RecyclerView也没有为Item开放OnItemClick等点击事件,这就需要开发者自己实现.博客最下面有Demo程序运行动画. 奉上Demo的Github链接. 在调研过程中,发现有同学修改RecyclerView源码来实现Item的点击监听,但认为这不是一个优雅的解决方案,最终决定在RecyclerView.ViewHolder上做文章. 思路是:因为ViewH

Android中实现全屏、无标题栏的两种办法(另附Android系统自带样式的解释)

原文:Android中实现全屏.无标题栏的两种办法(另附Android系统自带样式的解释) 在进行UI设计时,我们经常需要将屏幕设置成无标题栏或者全屏.要实现起来也非常简单,主要有两种方法:配置xml文件和编写代码设置. 1.在xml文件中进行配置 在项目的清单文件AndroidManifest.xml中,找到需要全屏或设置成无标题栏的Activity,在该Activity进行如下配置即可. 实现全屏效果: android:theme="@android:style/Theme.NoTitleB

android中ListView多次刷新重复执行getView的解决方法_Android

以前倒是没有注意listview的getView会重复执行多次,这次因为布局比较复杂,所以在测试的时候去断点跟踪,发现同一条数据不断的重复执行.觉得很奇怪,于是上网搜索了一下.网上的解释基本一致,就是ListView布局时height和width都不是fill_parent,导致不断计算高度,不断刷新.或者说它的父容器没有设置成fill_parent. 可以布局太复杂的情况下,全部按照fill_parent去调整不现实.所以想了另一种方案,就是动态固定高度. 在程序运行后,固定ListView的