国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義ListView單擊事件失效的解決方法

2019-10-21 21:45:33
字體:
來源:轉載
供稿:網友

因為自帶的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 屬性來覆蓋子類控件,而直接獲取焦點。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲沃县| 沾化县| 格尔木市| 永泰县| 临清市| 奉贤区| 江西省| 泽普县| 娄底市| 舞阳县| 阿克| 柳河县| 含山县| 那曲县| 大名县| 府谷县| 舒兰市| 赤峰市| 长顺县| 通江县| 达拉特旗| 萝北县| 武宣县| 梨树县| 泰来县| 潍坊市| 专栏| 吴旗县| 高唐县| 昭平县| 都匀市| 河北区| 土默特右旗| 吴堡县| 永福县| 苏州市| 河源市| 庆安县| 全椒县| 绥德县| 凤冈县|