Android ListView item 焦点被抢无法点击

通常,我们会自定义 ListView Item 的 Layout,当自定义的 Item Layout 含有主动获得焦点的控件时(例如 Button, ImageButton 等),那么我们就没办法点击 ListView Item 自己的点击事件。

解决办法很简单,只要在Item Layout 的根布局中加上 android:descendantFocusability= "blocksDescendants" 属性即可。如下方的 Item 布局文件所示:

Java

这里再说说descendantFocusability 这个属性的作用。

根据 官方说明 ,descendantFocusability 定义了 ViewGroup 和子控件的在获取焦点时的关系。

有下面三种取值:

  • beforeDescendants,ViewGroup 会在所有子控件之前获得焦点
  • afterDescendants,ViewGroup 会在所有子控件都不需要焦点时获得焦点
  • blocksDescendants,ViewGroup会阻断子控件获得焦点。

通常,我们只要把descendantFocusability 设置为blocksDescendants,即可解决由于 Item 里的 Button 抢夺焦点导致 Item 本身无法点击的问题。

更多相关文章

  1. android之animation(一)
  2. [转]Android自定义控件之TextView
  3. Android(安卓)RecyclerView设置点击事件
  4. Android中自定义AlertDialog使用
  5. Android(安卓)LayoutInflater的使用
  6. Android给控件添加触摸回调
  7. 迁移到 AndroidX 过程中遇到的各种问题
  8. Windows下安装 Android(安卓)Studio
  9. Android客制化------在设置中加入RAM flash计算

随机推荐

  1. 在Android Studio中,如何在app中打开链接
  2. android listview单击事件
  3. android 关闭外音
  4. Android 之使用LocalBroadcastManager解
  5. Android检测系统中是否存在某进程
  6. 分享:android之hardwareAccelerated你不知
  7. android   点击屏幕让软件盘消失
  8. Android xmpp 资源列表
  9. Android 之如何删除eclipse自动生成的//T
  10. android - Jni 接口函数表