android - 将CheckBox添加到ListView行 View 使该行不可点击

标签 android listview checkbox

我正在尝试创建一个类似于 Gmail 应用程序的 ListView,其中每一行的左侧都有一个 CheckBox,以便通过上下文操作栏进行多项选择和操作,同时允许单击每一行以转换到另一个 Activity 。

最初,在添加 CheckBox 之前,这些行将使用我正在使用的 Holo 主题中的浅蓝色背景颜色指示它们的选择,并在我的 中调用 onListItemClick >ListFragment 单击时,onItemLongClick 在我的 OnItemLongClickListener 中,当长按时。

当我将 CheckBox 添加到我的行 View 布局 .xml 文件时,背景不再改变颜色,我也不再收到点击或长按事件。如果我将 android:longClickable="true" 添加到我的 View .xml 中的顶部 ViewGroup ,那么我将再次开始获得长按事件。但是 android:clickable="true" 没有让我点击事件。并且都不允许背景蓝色选择指示返回。只有 CheckBox 本身在触摸时提供带有蓝色背景的视觉触摸指示。

我需要做什么才能获得正常的视觉选择指示以及 View 中带有 CheckBoxListView 行上的单击事件?

这是我的 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不久前关于这个主题,您可能会觉得有帮助。事实上,问题是添加可聚焦元素(如 CheckBoxButton)会禁用单击整个列表项的能力,并且必须禁用它们的聚焦性。您也不应向主布局添加任何 clickable 标志。

关于android - 将CheckBox添加到ListView行 View 使该行不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14290025/

相关文章:

android - 如何使用 Kotlin 在 Android 中保存开关设置

android - 如何在没有任何数据更改的情况下重绘 android ListView?

c# - ListView ItemActivate 延迟

PHP/Mysql 表单带有更多复选框,其中一些弹出问题重复

jquery - @Html.checkboxfor - 使用 id 获取复选框值

php - 使用 PHP 的 Foreach 循环在单独的文本框中显示数据库中的每个条目

android原生发送按钮图标

android - 如何添加适用于移动和非移动友好 URLS 的 Android WebView 设置?

Android Studio 2.4 错误 :platform-tools package is not installed and SDK auto-download is disabled

android - 将 ListView 行的颜色填充到一定百分比