Android自定义ListView单击事件失效的解决方法

 更新时间:2018年07月26日 15:12:10   作者:_York  
这篇文章主要为大家详细介绍了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 属性来覆盖子类控件,而直接获取焦点。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android基于Toolbar实现顶部标题栏及后退键

    Android基于Toolbar实现顶部标题栏及后退键

    这篇文章主要介绍了Android基于Toolbar实现顶部标题栏及后退键,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Kotlin启动协程的三种方式示例详解

    Kotlin启动协程的三种方式示例详解

    这篇文章主要为大家介绍了Kotlin启动协程的三种方式示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Android横竖屏切换及其对应布局加载问题详解

    Android横竖屏切换及其对应布局加载问题详解

    这篇文章主要为大家详细介绍了Android横竖屏切换及其对应布局加载问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Android仿微信朋友圈全文收起功能示例(附源码)

    Android仿微信朋友圈全文收起功能示例(附源码)

    本篇文章主要介绍了Android仿微信朋友圈全文收起功能示例(附源码),非常具有实用价值,需要的朋友可以参考下。
    2017-02-02
  • Android快速实现无预览拍照功能

    Android快速实现无预览拍照功能

    这篇文章主要为大家详细介绍了Android快速实现无预览拍照功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • Android 中cookie的处理详解

    Android 中cookie的处理详解

    这篇文章主要介绍了Android 中cookie的处理详解的相关资料,主要介绍Android 中Cookie 的操作,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-08-08
  • Android ViewPager实现选项卡切换

    Android ViewPager实现选项卡切换

    这篇文章主要介绍了Android ViewPager实现选项卡切换,详细分析了ViewPager实现选项卡切换功能,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • android使用include调用内部组件的方法

    android使用include调用内部组件的方法

    这篇文章主要介绍了android使用include调用内部组件的方法,涉及Android组件调用的相关技巧,需要的朋友可以参考下
    2015-05-05
  • Android实现照片墙效果的实例代码

    Android实现照片墙效果的实例代码

    Android实现照片墙效果的设计思路其实也非常简单,用一个GridView控件当作“墙”,然后随着GridView的滚动将一张张照片贴在“墙”上,这些照片可以是手机本地中存储的,也可以是从网上下载的
    2018-05-05
  • Android使用ViewPager快速切换Fragment时卡顿的优化方案

    Android使用ViewPager快速切换Fragment时卡顿的优化方案

    今天小编就为大家分享一篇关于Android使用ViewPager快速切换Fragment时卡顿的优化方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12

最新评论