Android-listview新增控制元件導致listview點選事件失效
阿新 • • 發佈:2019-01-23
今天寫了個demo,dialog自定義佈局添加了個listview,listview item裡面加了個checkBox,需求checkBox在列表裡有單選的效果:
測試的時候需要點選item也改變checkBox的選中狀態,然而listview的點選事件失效了。後來發現是失去焦點了。
最簡單的方法就是在item佈局跟佈局新增
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:orientation="horizontal" android:gravity="center_vertical" android:descendantFocusability="blocksDescendants" android:layout_height="50dp"> <CheckBox android:id="@+id/CheckBox" android:padding="5dp" android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imagee" android:padding="5dp" android:src="@mipmap/ic_launcher" android:layout_width="50dp" android:layout_height="50dp" /> <TextView android:text="名字" android:textSize="18sp" android:gravity="center" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:padding="5dp" android:layout_marginLeft="10dp" android:text="|" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="職位" android:textColor="#000000" android:textSize="18sp" android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
然後就完美解決了。