日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android 自定义ListView单击事件失效

發(fā)布時(shí)間:2025/7/14 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 自定义ListView单击事件失效 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?  因?yàn)樽詭У膌istView不能滿足項(xiàng)目需求,通過(guò)實(shí)現(xiàn)自己的Adapter去繼承ArrayAdapter 來(lái)實(shí)現(xiàn)自定義ListView的Item項(xiàng)目。

  出現(xiàn)點(diǎn)擊ListView的每一項(xiàng)都不會(huì)執(zhí)行setOnItemClickListener 里面的onItemClick 方法。

  原因是item里面存在一些子控件,默認(rèn)點(diǎn)擊獲取的焦點(diǎn)跑去子控件去了,點(diǎn)擊失效。

解決辦法:

  在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"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" android:padding="5dp"><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content"app:srcCompat="@drawable/message_oc" /><TextViewandroid:id="@+id/textTitle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="title"android:textSize="25dp"android:layout_marginLeft="15dp"/><TextViewandroid:id="@+id/textDate"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="right"android:text="date" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid: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>

?

該屬性是當(dāng)一個(gè)為view獲取焦點(diǎn)時(shí),定義viewGroup和其子控件兩者之間的關(guān)系。

屬性的值有三種:

??????? beforeDescendants:viewgroup會(huì)優(yōu)先其子類(lèi)控件而獲取到焦點(diǎn)

??????? afterDescendants:viewgroup只有當(dāng)其子類(lèi)控件不需要獲取焦點(diǎn)時(shí)才獲取焦點(diǎn)

??????? blocksDescendants:viewgroup會(huì)覆蓋子類(lèi)控件而直接獲得焦點(diǎn)

?

我們使用blocksDescendants 屬性來(lái)覆蓋子類(lèi)控件,而直接獲取焦點(diǎn)。

?

轉(zhuǎn)載于:https://www.cnblogs.com/tangchun/p/9367144.html

總結(jié)

以上是生活随笔為你收集整理的Android 自定义ListView单击事件失效的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。