listview或gridview中item的點選事件與控制元件點選事件衝突的解決
阿新 • • 發佈:2019-01-24
listview中item的點選事件與控制元件點選事件衝突的解決:
遇到的問題:listview條目點選事件無法處理事件。listview裡的控制元件搶先收到了事件。
方式1:設定包含所有控制元件的父view的 descendantFocusability屬性。android:descendantFocusability=”blocksDescendants”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="80dp"
android:layout_height="wrap_content" />
</LinearLayout>
2.方式二。設定 子控制元件的android:focusable=”false” 屬性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="80dp"
android:focusable="false"
android:layout_height="wrap_content" />
</LinearLayout>
總結來說,是事件分發與消費問題。由於時間限制,先列出解決方案。以後完善分析。