问题描述
我在使用 ListView
时遇到问题.它的项目(行)有一个 ImageButton
.ImageButton
设置了 android:onClick
,所以这个 onClick
事件有效,但点击行无效.
I have trouble with a ListView
. Its items (rows) have an ImageButton
.The ImageButton
has android:onClick
set, so this onClick
event is working, but click on row doesn't work.
如果我从行项目中删除 ImageButton
,单击行有效(ListView
具有正确的 onClick
侦听器).我该如何解决?当用户点击 ImageButton
时,我需要 onClick
事件,当用户选择行时需要标准的点击事件(不是点击 ImageButton
但单击该行).
If I remove the ImageButton
from the row item, click on row works (ListView
has correct onClick
listener). How can I fix it?I need onClick
event when the user clicks on the ImageButton
, and the standard click event when the user selects the row (not click the ImageButton
but click the row).
我的列表视图:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/restaurants_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@color/list_devider"
android:dividerHeight="1dp"
android:cacheColorHint="@color/list_background" />
推荐答案
很遗憾,
android:focusable="false"
android:focusableInTouchMode="false"
不适用于 ImageButton
.
我终于找到了解决方案这里一>.在这些项目的布局 xml 中,添加
I finally found the solution here. In your layout xml for those items, add
android:descendantFocusability="blocksDescendants"
到根视图.
它非常适合具有 ImageButton
的 ListView
.根据官方参考,blocksDescendants
意味着 ViewGroup
将阻止其后代接收焦点.
It works perfectly for a ListView
that has ImageButton
s. According to official reference, blocksDescendants
means that the ViewGroup
will block its descendants from receiving focus.
这篇关于无法单击带有图像按钮的 ListView 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!