问题一:Android中自定义ListView无法响应OnItemClickListener中的onItemClick方法问题解决方案

在Android软件设计与实现中我们通常都会使用到ListView这个控件,系统有一些预置的Adapter可以使用,例如SimpleAdapter和ArrayAdapter,但是总是会有一些情况我们需要通过自定义ListView来实现一些效果,那么在这个时候,我们通常会碰到自定义ListView无法选中整个ListViewItem的情况,也就是无法响应ListView的onItemClickListener中的onItemClick()方法,究竟是为什么呢?我之前也在网上查过不少的资料,但是没有发现什么有价值的文章,有一些是建议在Adapter的getView方法中对自己需要响应单击事件的控件进行设置。但是最终的效果并不是特别理想,而且我认为这是一种取巧的方式,并不推荐。之后自己查看了一下ViewGroup的源码,发现了以下的一段常量声明:

/** * This view will get focus before any of its descendants. */ public static final int FOCUS_BEFORE_DESCENDANTS = 0×20000; /** * This view will get focus only if none of its descendants want it. */ public static final int FOCUS_AFTER_DESCENDANTS = 0×40000; /** * This view will block any of its descendants from getting focus, even * if they are focusable. */ public static final int FOCUS_BLOCK_DESCENDANTS = 0×60000;

/** * This view will get focus before any of its descendants. */

public static final int FOCUS_BEFORE_DESCENDANTS = 0×20000;
/** * This view will get focus only if none of its descendants want it. */

public static final int FOCUS_AFTER_DESCENDANTS = 0×40000;
/** * This view will block any of its descendants from getting focus, even * if they are focusable. */

public static final int FOCUS_BLOCK_DESCENDANTS = 0×60000;

我们看到了一行代码定义的变量的意思是“当前View将屏蔽他所有子控件的Focus状态,即便这些子控件是可以Focus的”,其实这段话的意思就是这个变量代表着当前的View将不顾其子控件是否可以Focus自身接管了所有的Focus,通常默认能获得focus的控件有Button,Checkable继承来的所有控件,这就意味着如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我们可以通过将ListView中Item中包含的所有控件的focusable属性设置为false,这样的话ListView的Item自动获得了Focus的权限,也就可以被选中了,也就会响应onItemClickListener中的onItemClick()方法,然而将ListView的Item Layout的子控件focusable属性设置为false有点繁琐,我们可以通过对Item Layout的根控件设置其android:descendantFocusability=”blocksDescendant”即可,这样Item Layout就屏蔽了所有子控件获取Focus的权限,不需要针对Item Layout中的每一个控件重新设置focusable属性了,如此就可以顺利的响应onItemClickListener中的onItenClick()方法了。

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android上一些方法的区别和用法的注意事项
  5. 三、安卓UI学习(1)
  6. android实现字体闪烁动画的方法
  7. Android中dispatchDraw分析
  8. android用户界面之按钮(Button)教程实例汇
  9. Android四大基本组件介绍与生命周期

随机推荐

  1. 详解 Android(安卓)的 Activity 组件
  2. rotate旋转不间断动画
  3. Android中TextView的相应属性
  4. webservice android
  5. Android修改system只读权限:remount
  6. Android(安卓)菜单简析01(OptionsMenu)
  7. android Log图文详解(Log.v,Log.d,Log.i,
  8. 向eclipse中导入android中的sample样例+g
  9. Android(安卓)Camera 使用小结
  10. 理解Android(安卓)上的安全性