ItemClickListener不起作用.怎办?

来源:互联网 发布:普通话翻译客家话软件 编辑:程序博客网 时间:2024/04/28 02:01

 答案看最后的方案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 plaincopy
  1. android:descendantFocusability="blocksDescendants"  

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

[html] view plaincopy
  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.

ConstantValueDescriptionbeforeDescendants0The ViewGroup will get focus before any of its descendants.afterDescendants1The ViewGroup will get focus only if none of its descendants want it.blocksDescendants2The ViewGroup will block its descendants from receiving focus.

 



0 0
原创粉丝点击