有时,为了实现某种功能,在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. HTML中的标题<h1>标签的用法!

    在HTML中,<h1>标签是命名标题的元素,并且在网页中起到了很重要的作用,但是不要只是因为想要弄成黑粗字体就使用<h1>标题标签这样不好.如果说你还想要做完网站后需要进一步做 ...

  2. 【转】HTTP响应状态码参考簿

    HTTP响应状态码参考簿 http状态返回代码 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. http状态返回代码 代码   说明100   (继续) 请求者应当继续提出请求. ...

  3. K2 BPM_当K2遇上医药,用流程打通企业的任督二脉_业务流程管理系统

    据调查,如今仍有60%的医药企业,存在合规经营和利润下降的困扰,在“研”.“产”.“供”.“销”的运营过程中,时时伴随着严苛的管理政策和法规.如何加强企业跨部门.跨组织.跨业务线的执行能力,始终是管理 ...

  4. Traceback (most recent call last): File "../zubax_chibios/tools/make_boot_descriptor.py", line 251

    出现如下错误: Traceback (most recent call last): File "../zubax_chibios/tools/make_boot_descriptor.py ...

  5. MVC-路由(Route)

    1.启用路由前的准备工作 Global.asax.cs中注册路由 public class MvcApplication : System.Web.HttpApplication { protecte ...

  6. 深入浅出Git(偏向理论)

    目录 一.理论概述 1. 什么是Git 版本控制系统分类 2. GitLab和GitHub是什么 3.Git功能 二.结合具体命令了解其工作 1.环境 2.部署 Git仓库的使用 简单命令解释 Git ...

  7. Vue框架之初识

    介绍 vue.js 是用来构建web应用接口的一个库 技术上,Vue.js 重点集中在MVVM模式的ViewModel层,它连接视图和数据绑定模型通过两种方式.实际的DOM操作和输出格式被抽象的方式到 ...

  8. Hadoop读写mysql

    需求 两张表,一张click表记录某广告某一天的点击量,另一张total_click表记录某广告的总点击量 建表 CREATE TABLE `click` ( `id` ) NOT NULL AUTO ...

  9. 使用C#应用程序与Arduino开发板进行通信

    在本文中,我们将一个Arduino Pro Mini开发板连接到PC,并且使用C# Windows应用程序与其进行通信. 将硬件连接到PC是非常有利的,不仅是因为你可以发送命令以及监控状态,还可以实时 ...

  10. 从OkHttp的源码来看 HTTP

    先来了解一下OkHttp的历史,最早是square公司觉得Android给的HttpClient这块的库不太好用,于是乎做了一层包装,再后来他们包装的这个库被Android官方给收回去了,而Andro ...