1. 程式人生 > >Android 自定義OA平臺制作ListView單擊事件失效

Android 自定義OA平臺制作ListView單擊事件失效

rcc idt mes red left emc sde add XML

因為自帶新OA平臺制作QQ2952777280【話仙源碼論壇】hxforum.com【木瓜源碼論壇】papayabbs.com 的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">

android:id="@+id/imageView"
ImageView
android:id="@+id/imageView"
android:layout_height="wrap_content"

app:srcCompat="@drawable/message_oc" />

android:id="@+id/textTitle"
TextView
android:id="@+id/textTitle"
android:layout_height="wrap_content"
android:text="title"
android:textSize="25dp"
android:layout_marginLeft="15dp"/>

android:id="@+id/textDate"
TextView
android:id="@+id/textDate"
android:layout_height="wrap_content"
android:gravity="right"
android:text="date" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">

android:id="@+id/textMessage"
TextView
android:id="@+id/textMessage"
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 自定義OA平臺制作ListView單擊事件失效