天天看点

区分listview的item和Button的点击事件问题解决,两个click事件不再冲突了!

在ListView中加入Button这类的有 “点击” 事件的widget,发现原来listview的itemclick居然失效了,后来在网上查资料终于得以解决。

ListView 和 其它能触发点击事件的widget无法一起正常工作的原因是加入其它widget后,ListView的itemclick事件将无法触发,被其它widget的click事件屏蔽。

解决办法:

在item中,包含button的item的Layout中加入属性 android:descendantFocusability= “blocksDescendants”

在buttion的属性加入android:focusable=”false”

问题解决,两个click事件不再冲突了!

下面我是程序中的部分代码,该布局文件时listview中的item的布局,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/ListItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <ImageView
        android:layout_width="45dip"
        android:layout_height="45dip"
        android:layout_alignParentLeft="true"
        android:layout_gravity="center"
        android:background="@drawable/imageview_background"
        android:scaleType="fitXY" />

    <Button
        android:layout_width="@dimen/btn_attention_width"
        android:layout_height="@dimen/btn_attention_height"
        android:layout_alignParentRight="true"
        android:background="@drawable/button_selector_gradient"
        android:focusable="false"
        android:text="关注" />
</RelativeLayout>
           

继续阅读