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)都不會執(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會優(yōu)先其子類控件而獲取到焦點(diǎn)
- afterDescendants:viewgroup只有當(dāng)其子類控件不需要獲取焦點(diǎn)時(shí)才獲取焦點(diǎn)
- blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點(diǎn)
我們使用blocksDescendants 屬性來覆蓋子類控件,而直接獲取焦點(diǎn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵
這篇文章主要介紹了Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Android橫豎屏切換及其對應(yīng)布局加載問題詳解
這篇文章主要為大家詳細(xì)介紹了Android橫豎屏切換及其對應(yīng)布局加載問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android快速實(shí)現(xiàn)無預(yù)覽拍照功能
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)無預(yù)覽拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Android ViewPager實(shí)現(xiàn)選項(xiàng)卡切換
這篇文章主要介紹了Android ViewPager實(shí)現(xiàn)選項(xiàng)卡切換,詳細(xì)分析了ViewPager實(shí)現(xiàn)選項(xiàng)卡切換功能,感興趣的小伙伴們可以參考一下2016-02-02
android使用include調(diào)用內(nèi)部組件的方法
這篇文章主要介紹了android使用include調(diào)用內(nèi)部組件的方法,涉及Android組件調(diào)用的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Android實(shí)現(xiàn)照片墻效果的實(shí)例代碼
Android實(shí)現(xiàn)照片墻效果的設(shè)計(jì)思路其實(shí)也非常簡單,用一個(gè)GridView控件當(dāng)作“墻”,然后隨著GridView的滾動將一張張照片貼在“墻”上,這些照片可以是手機(jī)本地中存儲的,也可以是從網(wǎng)上下載的2018-05-05
Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案
今天小編就為大家分享一篇關(guān)于Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

