包含CheckBox的列表项布局无法按预期工作 [英] List item layout containing a CheckBox doesn't work as expected

查看:50
本文介绍了包含CheckBox的列表项布局无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView ,其布局包含一个 CheckBox .

我希望 CheckBox 都处于活动状态,并且希望列表项本身可以被点击.

它不起作用.

是的,我知道典型的解决方案(在 CheckBox 上将 focusable 设置为false,以使其不会失去焦点).这没有帮助.

这是我的列表项的布局:

 < RelativeLayoutxmlns:android ="http://schemas.android.com/apk/res/android"xmlns:tools ="http://schemas.android.com/tools"android:id ="@ + id/rl_container"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:padding ="8dp">< ImageViewandroid:id ="@ + id/img_icon"android:layout_width ="48dp"android:layout_height ="48dp"android:layout_margin ="8dp"android:contentDescription ="@ null"android:scaleType ="centerInside"android:visibility ="gone"/>< RelativeLayoutandroid:id ="@ + id/rl_texts"android:layout_width ="wrap_content"android:layout_height ="wrap_content"android:layout_toRightOf ="@ id/img_icon"android:layout_centerVertical ="true">< TextViewandroid:id ="@ + id/txt_name"android:layout_width ="wrap_content"android:layout_height ="wrap_content"android:textIsSelectable ="false"android:textColor ="@ color/black"/>< TextViewandroid:id ="@ + id/txt_subtitle"android:layout_width ="wrap_content"android:layout_height ="wrap_content"android:layout_below ="@ id/txt_name"android:textIsSelectable ="false"/></RelativeLayout><复选框android:id ="@ + id/cb_toggle_switch"android:focusable ="false"android:focusableInTouchMode ="false"android:layout_width ="wrap_content"android:layout_height ="wrap_content"android:layout_toLeftOf ="@ + id/img_arrow"android:layout_alignWithParentIfMissing ="true"android:layout_marginRight ="25dp"/>< ImageViewandroid:id ="@ + id/img_arrow"android:layout_width ="wrap_content"android:layout_height ="wrap_content"android:src ="@ drawable/arrow_button"android:layout_alignParentRight ="true"android:layout_centerVertical ="true"/></RelativeLayout> 

以下是托管列表的片段:

 < RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"android:id ="@ + id/content"android:layout_width ="match_parent"android:layout_height ="match_parent"><列表视图android:id ="@ + id/list"android:layout_width ="match_parent"android:layout_height ="match_parent"android:cacheColorHint =#00000000"android:clipToPadding ="false"android:divider ="@ null"android:dividerHeight ="0dp"android:listSelector =#00000000"android:paddingBottom ="60dp"/></RelativeLayout> 

非常简单.

仅从布局中删除 CheckBox 即可解决问题(显然,列表项的可映射性;不过,我当时没有复选框).

CheckBox Visibility 设置为 GONE (我实际上需要对某些项目执行)并不能解决该问题.

OnItemClickListener 根本不起作用.

OnClickListener 项- rl_container (整个列表项的根视图)和 cb_toggle_switch (复选框)种类有效.

这似乎不是确定性的.多次单击有时会触发容器和复选框的点击侦听器,并且一旦在复选框中连续单击几次,就可以强制切换其状态.但这不是立即的.

这使我怀疑幕后是否有一些关于焦点的复杂逻辑,但是我将 OnFocusChangeListener s附加到了容器和复选框上,甚至从未收到通知.

我尝试摆弄 descendantFocusability (在 ListView 上),禁用并启用容器(根)布局的可聚焦性和可单击性.

删除所有子视图,仅保留容器和复选框会产生相同的问题.

有什么想法吗?你会尝试什么?

解决方案

如果在按列表项时不想选择它,则可以将属性添加到复选框属性: android:enabled ="false"

I have a ListView whose layout contains a CheckBox.

I want both the CheckBox to be active, and list items themselves to be tappable.

It doesn't work.

Yes, I know about the typical solution (setting focusable to false on the CheckBox so that it doesn't steal focus). It doesn't help.

Here's the layout of my list item:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/rl_container"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_margin="8dp"
        android:contentDescription="@null"
        android:scaleType="centerInside"
        android:visibility="gone"/>

    <RelativeLayout
        android:id="@+id/rl_texts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/img_icon"
        android:layout_centerVertical="true">

        <TextView
            android:id="@+id/txt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textIsSelectable="false"
            android:textColor="@color/black"/>

        <TextView
            android:id="@+id/txt_subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txt_name"
            android:textIsSelectable="false"/>
    </RelativeLayout>

    <CheckBox
        android:id="@+id/cb_toggle_switch"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/img_arrow"
        android:layout_alignWithParentIfMissing="true"
        android:layout_marginRight="25dp"/>

    <ImageView
        android:id="@+id/img_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_button"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

Here's the fragment hosting the list:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"

        android:clipToPadding="false"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:listSelector="#00000000"
        android:paddingBottom="60dp"/>

</RelativeLayout>

Pretty simple.

Only removing CheckBox from the layout fixes the problem (tappability of the list items, obviously; I have no checkbox then, though).

Setting Visibility of the CheckBox to GONE (which I actually have to do for some items) doesn't fix it.

OnItemClickListener doesn't work at all.

OnClickListener of items - rl_container (the root view of the entire list item) and cb_toggle_switch (the checkbox) kind of works.

It's seems not to be deterministic. Clicking repeatedly sometimes fires the click listener of the container and the checkbox, and once you click several times in a row on the checkbox, you're able to force switching its state. But it's never immediate.

This made me suspect that behind the scenes we've got some complex logic regarding focus, but I attached OnFocusChangeListeners to both the container and the checkbox and they are never even notified.

I tried fiddling with descendantFocusability (on the ListView), disabling and enabling focusability and clickability of the container (root) layout.

Removing all subviews and leaving only the container and the checkbox leaves the same problem.

Any ideas? What would you try?

解决方案

If you don't want select it when you press on your list item, you can add an attribute to the checkbox attributes: android:enabled="false"

这篇关于包含CheckBox的列表项布局无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆