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

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的事件监听都可以正常使用!

(记录一下这个小笔记) 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值