ItemClickListener不起作用.怎办?

 答案看最后的方案3


    listview在android开发中很地方都用到了,通常我们需要定制item里面的视图,就要重写adapter。

而item中的控件根据需要来添加。但是如果出现了某些特定的item控件设置如onClickListener,就可能导致listview 的onItemClickListener不起作用。

有趣的是,有时我们同时为Listview添加了setOnTouchListener( touchListener ).,,onItemClickListener()... 

三管齐下.到底会发生什么有趣的事呢..?

 

   出现的onItemClickListener不能响应的原因是在item中有button类(子类)或者checkable类(子类)控件,他们两个的优先级高于前者.导致他们两个抢了焦点.而且不会传递着个点击事件..导致了item的焦点在子项的控件上,处理的办法就是将子项的控件焦点去掉,同时在item中xml设置阻止子项获得焦点的属性,即可解决尚需问题

方案1:

   综述: 出现onItemClickListener不能响应,原因可能有多种,本人总结了有两种情况,

一种是isEnable中返回值为false导致不能点击和选择,

一种是因为item中有了checkable或者button类(子类)控件导致了item的焦点失去,从不能响应。

因此需要仔细分析,问题导致的具体原因,才更好的解决问题。

 来源:http://www.cnblogs.com/xilinch/archive/2012/11/07/2759265.html


方案2:

   这是一个折中的方案..我们可以把对ITEM设置的onClickListener放到每个view里面的控件去..


方案3:最正确合理的..

解决方案

在ListView要显示的Item的外层加上

[html]  view plain copy
  1. android:descendantFocusability="blocksDescendants"  

假设我们的listview里面的每个item的layout是这段代码

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal"   
  6.     android:descendantFocusability="blocksDescendants">  
  7.   
  8.     <TextView  
  9.         android:id="@+id/checkinfo_item_name"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_gravity="left"  
  14.         android:textColor="@android:color/black"  
  15.         android:textSize="25sp" />  
  16.   
  17.     <Spinner  
  18.         android:id="@+id/checkinfo_item_value"  
  19.         style="@style/SpinnerAsEditText"  
  20.         android:layout_width="125dip"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignParentRight="true"  
  23.         android:focusable="false" />  
  24.   
相关解释:

android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

Constant Value Description
beforeDescendants 0 The ViewGroup will get focus before any of its descendants.
afterDescendants 1 The ViewGroup will get focus only if none of its descendants want it.
blocksDescendants 2 The ViewGroup will block its descendants from receiving focus.

 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值