zoukankan      html  css  js  c++  java
  • Android 自定义ListView单击事件失效

       因为自带的listView不能满足项目需求,通过实现自己的Adapter去继承ArrayAdapter 来实现自定义ListView的Item项目。

      出现点击ListView的每一项都不会执行setOnItemClickListener 里面的onItemClick 方法。

      原因是item里面存在一些子控件,默认点击获取的焦点跑去子控件去了,点击失效。

    解决办法:

      在item的根目录加入android:descendantFocusability="blocksDescendants"

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:descendantFocusability="blocksDescendants">
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:padding="5dp">
    
    
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/message_oc" />
    
            <TextView
                android:id="@+id/textTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="title"
                android:textSize="25dp"
                android:layout_marginLeft="15dp"/>
    
            <TextView
                android:id="@+id/textDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="date" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/textMessage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textMultiLine"
                android:text="message"
                android:textSize="20dp"/>
        </LinearLayout>
    
    </LinearLayout>

    该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。

    属性的值有三种:

            beforeDescendants:viewgroup会优先其子类控件而获取到焦点

            afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

            blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

    我们使用blocksDescendants 属性来覆盖子类控件,而直接获取焦点。

  • 相关阅读:
    织网的日子里——第一章:TCP时间获取之客户端和服务器端程序
    Hdu 4517 小小明系列故事——游戏的烦恼
    MongoDB安装配置
    【算法学习】线性时间排序计数排序、基数排序和桶排序详解与编程实现
    C++中宏的使用技巧
    每三小时热备shell脚本文件
    OSS音频编程实例
    MySQL的Illegal mix of collationsy异常原因和解决方法
    SQL用了Union后的排序问题
    union 时只能查出一个表中的信息,另一个表只能查出字段
  • 原文地址:https://www.cnblogs.com/tangchun/p/9367144.html
Copyright © 2011-2022 走看看