onItemClickListener不起作用解决办法(原创)

简介: 最近写了一个项目,界面使用的是帧布局,里面放置了listview显示联系人,以及右侧有对联系人的字母索引定位。 结果在对联系人listview设置onItemClickListener时,发现竟然不起作用。

  最近写了一个项目,界面使用的是帧布局,里面放置了listview显示联系人,以及右侧有对联系人的字母索引定位。

结果在对联系人listview设置onItemClickListener时,发现竟然不起作用。

  下面的是布局文件以及设置代码

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
     android:id="@+id/contact_fl"

> <ListView android:id="@+id/contact_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.0" android:focusable="true" android:focusableInTouchMode="true" android:dividerHeight="1px" android:scrollbars="none" /> <LinearLayout android:layout_width="28dp" android:layout_height="match_parent" android:layout_gravity="right|center" android:layout_marginBottom="6dp" android:layout_marginTop="10dp" android:gravity="right" android:orientation="vertical" > <ListView android:id="@+id/contact_letter" android:layout_width="28dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:focusable="true" android:focusableInTouchMode="true" android:scrollbars="none" /> </LinearLayout> </FrameLayout>

    在activity中设置onItemCllickListener,
代码如下:

  lv_contact.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
                Log.i("my", "onItemClick clicked");
                
            }
            
        });

  item的布局如下:

<ImageView 
android:scaleType="centerInside"
android:layout_marginLeft="12dp"
android:layout_marginTop="5dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="3dp"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_contact_picture"
android:id="@+id/contact_contactinfoitem_iv_photo"
/> 
  
<TextView  
android:singleLine="true"
android:ellipsize="marquee"
android:textStyle="bold"
android:marqueeRepeatLimit="marquee_forever"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:id="@+id/contact_contactinfoitem_tv_name"
android:text="xxx"
/> 
<TextView  
android:singleLine="true"
android:ellipsize="marquee"
android:textStyle="bold"
android:marqueeRepeatLimit="marquee_forever"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:id="@+id/contact_contactinfoitem_tv_phone"
android:text="xxx"
/>

 

     

 

当点击联系人listiew的时候,没有打出对应的log,非常奇怪,开始的时候还以为是设置了onTouchListener的原因,因为onTouchListener返回结果会影响是否需要继续处理屏幕事件。
   检查发现onTouchListener里面,返回结果是false,不是true,这意味着屏幕事件是继续传递处理的,不会影响到onItemClickListener的处理。

     去网上查了下发现有说  xml中有个焦点属性会影响onTouchListener,需要将其改为false

     再次检测xml文件,里面确实设置有这两个属性

android:focusable="true"
android:focusableInTouchMode="true"

这两个属性的看名字就知道到意思,
android:focusable="true"-------
       设置是否获得焦点。若有requestFocus()被调用时,后者优先处理。注意在表单中想设置某一个如EditText获取焦点,光设置这个是不行的,需要将这个EditText前面的focusable都设置为false才行。在Touch模式下获取焦点需要设置focusableInTouchMode为true

android:focusableInTouchMode="true"----

     设置在Touch模式下View是否能取得焦点。

     在xml中修改,这属性为false,运行工程,发现还是一样的onItemCllickListener不起作用,这就纠结了。

  因为赶时间,干脆在adapter中,写item的onclicklistener算了。代码如下:

  

contact_fl.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View view)
            {
                Log.i("my", "onClick clicked");
                
            }
            
        });

 

       运行项目,item的点击效果有了。但是同时有新的问题出现了。在我的ontouchListener种,onkeydown事件消失了。。。。

只有onkeymove和onkeyup。

    经过分析得出结论,那就是肯定有方法或者属性,设置的时候冲突了,item的布局文件中没有button之类的空间。重点查看是否有方法或者属性使得click事件消失了

     再次到adapter中仔细检查,发现有个两个这样的方法:

@Override
    public boolean areAllItemsEnabled()
    {
        // all items are separator
        return false;
    }

    @Override
    public boolean isEnabled(int position)
    {
        // all items are separator
        return false;
    }

    查看说明:

      Returns true if the item at the specified position is not a separator. (A separator is a non-selectable, non-clickable item). The result is unspecified if position is invalid. An ArrayIndexOutOfBoundsException should be thrown in that case for fast failure.

    意思就是说,如果当前指定的位置不是一个separator--分隔符(分隔符是一个不能选中,不能点击的item),那么返回true。

    那么赶紧改为true,运行项目,效果都有了。

  成功解决。

  

 

 

 

 

 

目录
相关文章
|
9月前
|
存储 安全 Java
Spring Boot 编写 API 的 10条最佳实践
本文总结了 10 个编写 Spring Boot API 的最佳实践,包括 RESTful API 设计原则、注解使用、依赖注入、异常处理、数据传输对象(DTO)建模、安全措施、版本控制、文档生成、测试策略以及监控和日志记录。每个实践都配有详细的编码示例和解释,帮助开发者像专业人士一样构建高质量的 API。
282 9
|
9月前
|
SEO
为什么大家都推荐PageAdmin CMS?
PageAdmin CMS是一款功能强大、易于使用、免费开源的网站管理系统,适用于政务、企业、学校等门户网站,具备丰富的功能插件和SEO功能,是建站者的理想选择。
243 5
|
11月前
|
边缘计算 5G vr&ar
|
11月前
|
机器学习/深度学习 监控 搜索推荐
电商平台如何精准抓住你的心?揭秘大数据背后的神秘推荐系统!
【10月更文挑战第12天】在信息爆炸时代,数据驱动决策成为企业优化决策的关键方法。本文以某大型电商平台的商品推荐系统为例,介绍其通过收集用户行为数据,经过预处理、特征工程、模型选择与训练、评估优化及部署监控等步骤,实现个性化商品推荐,提升用户体验和销售额的过程。
496 1
|
11月前
|
机器学习/深度学习 人工智能 安全
如何实现基于AI的网站安全防护?一文讲解
如何实现基于AI的网站安全防护?一文讲解
143 2
|
存储 机器学习/深度学习 数据可视化
浅谈数字化和可视化的区别、各自的优缺点及未来的趋势主流
浅谈数字化和可视化的区别、各自的优缺点及未来的趋势主流
1081 2
|
数据安全/隐私保护
使用NPS服务器内网穿透——实现远程访问外网电脑(三)
使用NPS服务器内网穿透——实现远程访问外网电脑(三)
757 0
|
Kubernetes 安全 程序员
解锁GitHub新姿势·只需一个.打开Vscode在线编辑代码
程序员的笔记软件,应该满足以下几个条件
解锁GitHub新姿势·只需一个.打开Vscode在线编辑代码
|
网络协议 网络架构
ICMP 是个啥破玩意?(一)
ICMP 的全称是 Internet Control Message Protocol(互联网控制协议),它是一种互联网套件,它用于IP 协议中发送控制消息。也就是说,ICMP 是依靠 IP 协议来完成信息发送的,它是 IP 的主要部分,但是从体系结构上来讲,它位于 IP 之上,因为 ICMP 报文是承载在 IP 分组中的,就和 TCP 与 UDP 报文段作为 IP 有效载荷被承载那样。
ICMP 是个啥破玩意?(一)
|
存储 弹性计算 前端开发
阿里无影云桌面测评
阿里无影云桌面测评
阿里无影云桌面测评