Item的click事件和Item里的button等子控件的click事件同时生效的解决办法

ListView setOnItemClickListener事件和ListView中Item中包含的子控件(比如button)的click事件共存的解决办法:
在ListView的item的xml配置文件的根节点添加属性
android:descendantFocusability="blocksDescendants"
并且,在要添加事件的子控件(如button)的属性里添加 android:focusable="false"
另外,注意:有时现成的几个adapter满足不了要求,此时就需要继承自BaseAdapter。

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

listview_listitem_layout.xml   代码如下:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  android:descendantFocusability="blocksDescendants"
  style="@style/ListItem">  
    
    <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_gravity="center"
   android:layout_alignParentLeft="true"
 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:text="关注"
  android:focusable="false"/>
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值