Android自定義ListView單擊事件失效的解決方法
因?yàn)樽詭У膌istView不能滿足項(xiàng)目需求,通過實(shí)現(xiàn)自己的Adapter去繼承ArrayAdapter 來實(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"> <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>
該屬性是當(dāng)一個(gè)為view獲取焦點(diǎn)時(shí),定義viewGroup和其子控件兩者之間的關(guān)系。
屬性的值有三種:
- beforeDescendants:viewgroup會(huì)優(yōu)先其子類控件而獲取到焦點(diǎn)
- afterDescendants:viewgroup只有當(dāng)其子類控件不需要獲取焦點(diǎn)時(shí)才獲取焦點(diǎn)
- blocksDescendants:viewgroup會(huì)覆蓋子類控件而直接獲得焦點(diǎn)
我們使用blocksDescendants 屬性來覆蓋子類控件,而直接獲取焦點(diǎn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android11文件管理權(quán)限申請?jiān)敿?xì)介紹
大家好,本篇文章主要講的是Android11文件管理權(quán)限申請?jiān)敿?xì)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android實(shí)現(xiàn)京東App分類頁面效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)京東App分類頁面效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Kotlin注解實(shí)現(xiàn)Parcelable序列化流程詳解
有時(shí)我們會(huì)在界面跳轉(zhuǎn)的過程中,做對象傳值,這時(shí)就需要對該對象做序列化處理了。Android中對對象的序列化處理有兩種方式,這篇文章主要介紹了Kotlin注解實(shí)現(xiàn)Parcelable序列化2022-12-12Android三種網(wǎng)絡(luò)通訊方式及Android的網(wǎng)絡(luò)通訊機(jī)制
在android平臺(tái)目前提供了三種網(wǎng)絡(luò)接口可以使用:分別是java.net.*(標(biāo)準(zhǔn)Java接口)、Org.apache接口和Android.net.*(Android網(wǎng)絡(luò)接口),本文主要給大家介紹android三種網(wǎng)絡(luò)通訊方式及android的網(wǎng)絡(luò)通訊機(jī)制,小伙伴們一起學(xué)習(xí)吧2015-11-11Android實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽及其點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽及其點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法
這篇文章主要介紹了Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法,涉及Android布局、窗口、事件監(jiān)聽、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容
這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容,主要通過設(shè)置EditText的setTransformationMethod()方法來實(shí)現(xiàn),需要的朋友可以參考下2014-09-09Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解2
這篇文章主要為大家進(jìn)一步的介紹了Android開源項(xiàng)目PullToRefresh下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09