ListView的Item布局子控件与Item的setOnItemClickListenter监听事件冲突失效的解决方法

来源:互联网 发布:阿里云远程登录用户名 编辑:程序博客网 时间:2024/06/06 15:01

通过查找google官方提供的Api文档的ViewGroup类查看下面源码

  public voidsetDescendantFocusability(int focusability)

Set the descendant focusability of this view group. This defines the relationship between this view group and its descendants when looking for a view to take focus inrequestFocus(int, android.graphics.Rect).

Parameters
focusability one of FOCUS_BEFORE_DESCENDANTS,FOCUS_AFTER_DESCENDANTS,FOCUS_BLOCK_DESCENDANTS通过该方法设置ViewGroup的子控件的获取焦点的能力,三个参数分别为:
FOCUS_BEFORE_DESCENDANTS      在子控件前获取焦点;
 FOCUS_AFTER_DESCENDANTS  在子控件之后获取焦点;
FOCUS_BLOCK_DESCENDANTS  阻止任何的子控件获得焦点,即使他们是focusable。  

解决监听事件冲突的方法:

在ListView的Item布局xml文件中进行如下的设置android:descendantFocusability="blocksDescendants"的属性
?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"    android:background="#fd6563"    android:descendantFocusability="blocksDescendants">
   // ....子控件
</RelativeLayout>

这样ListView的Item布局子控件与Item的setOnItemClickListenter监听事件都能生效了

0 0