Android中ListView的條目中有button時,item的點擊事件失效。
做了一個列表,使用listview實現,item的佈局也很簡答,可是設置item的監聽室不管用了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_48"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/item_list_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_14"
tools:text="償付能力-債券與權益比" />
<RadioButton
android:id="@+id/item_list_radiobtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:button="@drawable/radio_button_style_knowledgechild" />
</LinearLayout>
後來考慮到是這個radiobutton有可能搶佔了焦點,所以重新改了下佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_48"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/item_list_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp_14"
tools:text="償付能力-債券與權益比" />
<RadioButton
android:id="@+id/item_list_radiobtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:button="@drawable/radio_button_style_knowledgechild"
android:focusable="false" />
</LinearLayout>
- 然後的然後,就好使了,嘿嘿,總結一下
1.button的點擊事件要寫在自定義適配器中。(如果只是展示的話,就不用寫事件監聽)
2.ListView條目點擊事件要寫在Activity中
3.button的屬性中必須要寫 android:focusable="false"
4.在Item佈局的根佈局加上android:descendantFocusability="blocksDescendants"的屬性就好了,至此listview點擊的靈異事件告一段落。心得:遇到不會不懂的地方除了網上查詢資料之外,也可以多多去嘗試每種屬性的作用,多閱讀官方文檔(我始終覺得還是讀原文的比翻譯的理解的會更好)。
經過以上處理,item的事件監聽和button的事件監聽都可以正常使用!
(記錄一下這個小筆記)