Android ListView item有button时,item点击事件失效

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40543575/article/details/89960395

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. 然后的然后,就好使了,嘿嘿,总结一下

1.button的点击事件要写在自定义适配器中。(如果只是展示的话,就不用写事件监听)

2.ListView条目点击事件要写在Activity中

3.button的属性中必须要写 android:focusable="false"

4.在Item布局的根布局加上android:descendantFocusability="blocksDescendants"的属性就好了,至此listview点击的灵异事件告一段落。心得:遇到不会不懂的地方除了网上查询资料之外,也可以多多去尝试每种属性的作用,多阅读官方文档(我始终觉得还是读原文的比翻译的理解的会更好)。

经过以上处理,item的事件监听和button的事件监听都可以正常使用!

(记录一下这个小笔记) 

猜你喜欢

转载自blog.csdn.net/qq_40543575/article/details/89960395