有时,为了实现某种功能,在Android程序中会考虑在ListView的每一个Item中添加一个Button(或ImageButton等)。

但是,这样会出现一个问题:

当同时设置了Button的onClick及ListView的onItemClickListener时,ListView的onItemClickListener会失去作用,即ListView不响应Item的点击事件。

出现这个问题的原因是:

ListView的Item能被选中的原因是它能获取Focus,而自定义的Item中有Button或者ImageButton等自控件时,子控件会获取默认Focus,这样Item就获取不到Focus,自然也无法相应点击事件了。

解决方法很简单:

在Item的布局的根Layout上加上:

1
android:descendantFocusability="blocksDescendants"

即可,可参照如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e7e7e7"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="vertical" > <ImageView
android:id="@+id/app_icon"
android:layout_width="@dimen/app_icon_size"
android:layout_height="@dimen/app_icon_size"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:scaleType="fitCenter" /> <com.dx.widget.DXCustomButton
android:id="@+id/uninstall_btn"
android:layout_width="wrap_content"
android:layout_height="32dip"
android:layout_gravity="center_vertical"
android:background="@drawable/btn2"
android:text="@string/uninstall_app"
android:textSize="12sp" /> </LinearLayout>

但是当这时,会出现另外一个问题,当点击每一个Item时(不是点击Button),Item中的Button也会呈现按下的状态。解决这个问题的方法也很简单,重写你所需的Button中的setPressed方法即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class DXCustomButton extends Button {
public DXCustomButton(Context context) {
super(context);
} public DXCustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
} public DXCustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} @Override
public void setPressed(boolean pressed) {
if (pressed && ((View) getParent()).isPressed()) {
return;
}
super.setPressed(pressed);
} }

ListView中的Item不能点击的解决方法的更多相关文章

  1. ListView中响应item的点击事件并且刷新界面

    ---恢复内容开始--- 最近在在实现listview功能中遇到了这个问题: 点击事件写在了adapter的item中,不知道如何在listview的点击事件中更新数据的显示: 总结:1.要使用not ...

  2. Android处理ListView中的Item中的Button按钮不能点击的问题

    问题描述:ListView列表中的Button按钮按钮不能点击 解决办法:在ListView中的Item项的布局文件中加上:android:descendantFocusability="b ...

  3. listview指定某item的点击效果

    需求:listview的某些item能够点击,需要点击效果,有些item不能点击,需要屏蔽点击效果. 实现: 1.layout: <ListView android:id="@+id/ ...

  4. Android 关于ListView中按钮监听的优化问题(方法二)

    关于ListView中按钮监听的优化问题(方法一)地址: http://www.cnblogs.com/steffen/p/3951901.html 之前的方法一,虽然能够解决position的传递, ...

  5. jquery ui中 accordion的问题及我的解决方法

    原文:jquery ui中 accordion的问题及我的解决方法 jquery有一套所谓的ui组件,很不错的.如果有兴趣的朋友,可以参考http://jqueryui.com/ 但其中的accord ...

  6. PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等

    页面导航: 首页 → 网络编程 → PHP编程 → php技巧 → 正文内容 PHP安全 PHP开发中常见的安全问题详解和解决方法(如Sql注入.CSRF.Xss.CC等) 作者: 字体:[增加 减小 ...

  7. ios UITableView中Cell重用机制导致内容重复解决方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  8. WAMP中phpMyAdmin登陆不了问题的解决方法

    WAMP中phpMyAdmin登陆不了问题的解决方法

  9. 问题-[Access]“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法

    问题现象:ado.net oledb方式访问Access数据库文件时报错“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法  问题处理:1.数据库名称不能命名为:Syste ...

随机推荐

  1. 对于vue中axios拦截器简单封装

    axios.interceptors.response.use( response => { // 如果返回的状态码为200,说明接口请求成功,可以正常拿到数据 // 否则的话抛出错误 if ( ...

  2. 十三、细说NULL导致的神坑,让人防不胜防

    当数据的值为NULL的时候,可能出现各种意想不到的效果,让人防不胜防,我们来看看NULL导致的各种神坑,如何避免? 一.比较运算符中使用NULL 任何值和NULL使用运算符(>.<.> ...

  3. 初始化springbean

    public class SMSMessageHandler implements InitializingBean { @Overridepublic void afterPropertiesSet ...

  4. Spark foreachpartiton和mappartition的异同

    相同 都是对分区进行操作 不同 1.foreachpartition是Action操作,mappartition是Transformation操作 2.foreachpartition无返回值,map ...

  5. PAT基础级-钻石段位样卷2-7-4 6翻了 (15 分)

    “666”是一种网络用语,大概是表示某人很厉害.我们很佩服的意思.最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思.如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字 ...

  6. POJ 2155 Matrix[树状数组+差分]

    原题链接:https://vjudge.net/problem/POJ-2155 题目大意 给定 n* n 矩阵A,其元素为0或1. A [i][j] 表示第i行和第j列中的数字.最初全为0. 我们有 ...

  7. 一、冒泡排序Bubble sort

    https://www.cnblogs.com/kkun/archive/2011/11/23/2260312.html#3824357 #include<iostream> #inclu ...

  8. 《ABCD组》第四次作业:项目需求调研与分析

    <ABCD组>第四次作业:项目需求调研与分析 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https:// ...

  9. python基础--文件控制

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...

  10. python-Redis cluster基础指标监控

    #!/usr/local/python/shims/python from rediscluster import StrictRedisCluster ''' 需要在宿主机python中安装redi ...