答案看最后的方案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的外层加上
- android:descendantFocusability="blocksDescendants"
假设我们的listview里面的每个item的layout是这段代码
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:descendantFocusability="blocksDescendants">
- <TextView
- android:id="@+id/checkinfo_item_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_gravity="left"
- android:textColor="@android:color/black"
- android:textSize="25sp" />
- <Spinner
- android:id="@+id/checkinfo_item_value"
- style="@style/SpinnerAsEditText"
- android:layout_width="125dip"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:focusable="false" />
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. |