【问题标题】:Listview with checkbox issue带有复选框问题的列表视图
【发布时间】:2012-02-14 22:31:34
【问题描述】:

我有一个ListView,它为每一行膨胀一个包含CheckBox 和更多TextViews 的xml,这些RelativeLayout 中。 问题是我不知道如何将 onClick 事件从复选框传递到后排。 我想实现这种行为:用户按下复选框并按下整个列表行。如果我为每一行充气android.R.layout.simple_list_item_multiple_choice,我就会看到这种行为,但如果没有这个 android 特定的布局,我无法弄清楚如何做到这一点。

谁能给我一个想法或方向?

下面是代码:

列表视图:

<ListView
    android:id="@+id/include_sent_list_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:cacheColorHint="@color/white"
    android:layout_alignParentTop="true"/>

以及为每一行膨胀的xml:

<RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

<CheckBox
        android:id="@+id/checked_radio_button"
        android:layout_width="50dp"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        />
<TextView
        android:id="@+id/account_number_text"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        style="@style/config_simple_small_text_view_style"
        android:paddingTop="15dp"
        android:layout_toRightOf="@id/checked_radio_button"

        />
<TextView
        android:id="@+id/account_name_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        style="@style/config_simple_small_text_view_style"
        android:paddingTop="15dp"
        android:layout_toRightOf="@id/account_number_text"/>

</RelativeLayout>

【问题讨论】:

    标签: android listview checkbox


    【解决方案1】:

    复选框消耗列表项的焦点。您需要在布局文件中进行设置:

    <!-- Must make this non-focusable otherwise it consumes  -->  
    <!-- events intended for the list item (i.e. long press) -->
    <CheckBox
        android:id="@+id/item_entry_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        />
    

    【讨论】:

    • 我试过了,但似乎不起作用。我想这不会发生,因为我将这个视图膨胀到列表行
    • 看起来不错。 .java 怎么样?
    • .java 代码看起来像一个正常的膨胀过程。屏幕正确充气,但复选框获取点击事件。 android.R.layout.simple_list_item_multiple_choice 可以以某种方式设法实现这一点,但我不知道如何
    猜你喜欢
    • 2011-07-23
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多