我正在尝试创建一个类似于 Gmail 应用程序的 ListView
,其中每一行的左侧都有一个 CheckBox
,以便通过上下文操作栏进行多项选择和操作,同时允许单击每一行以转换到另一个 Activity 。
最初,在添加 CheckBox
之前,这些行将使用我正在使用的 Holo 主题中的浅蓝色背景颜色指示它们的选择,并在我的 中调用
单击时,onListItemClick
>ListFragmentonItemLongClick
在我的 OnItemLongClickListener
中,当长按时。
当我将 CheckBox
添加到我的行 View 布局 .xml 文件时,背景不再改变颜色,我也不再收到点击或长按事件。如果我将 android:longClickable="true"
添加到我的 View .xml 中的顶部 ViewGroup
,那么我将再次开始获得长按事件。但是 android:clickable="true"
没有让我点击事件。并且都不允许背景蓝色选择指示返回。只有 CheckBox
本身在触摸时提供带有蓝色背景的视觉触摸指示。
我需要做什么才能获得正常的视觉选择指示以及 View 中带有 CheckBox
的 ListView
行上的单击事件?
这是我的 View .xml 的相关部分:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="true"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingRight="12dp"
android:paddingTop="5dp" >
...
</LinearLayout>
</LinearLayout>
最佳答案
我写了一个blog post不久前关于这个主题,您可能会觉得有帮助。事实上,问题是添加可聚焦元素(如 CheckBox
或 Button
)会禁用单击整个列表项的能力,并且必须禁用它们的聚焦性。您也不应向主布局添加任何 clickable
标志。
关于android - 将CheckBox添加到ListView行 View 使该行不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14290025/