首页 > 其他 > 详细

ListView中Item与Checkable子类控件抢焦点问题

时间:2017-10-11 21:40:21      阅读:210      评论:0      收藏:0      [点我收藏+]

Android开发中,经常需要为ListView定制Adapter,绑定各种子类控件。
如果Item包含Button等Checkable的控件,那么就会发生点击Item无法响应的问题。原因是自己定义的Item中Button等Checkable控件先获取到了焦点。

解决方案有两种:

1.在ListView的Item的xml文件的根元素如LinearLayout中添加属性

android:descendantFocusability="blocksDescendants"
该属性定义了当View获取焦点时,viewGroup与子控件的关系:
beforeDescendants:viewgroup会优先子类控件而获取到焦点
afterDescendants:viewgroup当子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
(测试第三个可以,第一个没反应。。。)

2.在Checkable控件中添加属性:
android:focusable="false"
android:clickable="true"
添加位置
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2  android:id="@+id/linearLayout"
 3  android:layout_width="match_parent" 
 4  android:layout_height="match_parent" 
 5  android:orientation="horizontal" 
 6  android:descendantFocusability="blocksDescendants" > 
 7  <Button android:id="@+id/button" 
 8      android:layout_width="50dp" 
 9      android:layout_height="100dp" 
10      android:focusable="false" 
11      android:clickable="true" 
12      android:text="按钮"/>
13  <TextView 
14      android:id="@+id/textView" 
15      android:layout_width="50dp" 
16      android:layout_height="100dp"
17      android:focusable="false" 
18      android:clickable="true"
19      android:text="文本" />
20 </LinearLayout>

 

 

ListView中Item与Checkable子类控件抢焦点问题

原文:http://www.cnblogs.com/ganchuanpu/p/7652864.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!