简体   繁体   中英

Listview with checkbox issue

I have a ListView that inflate for each row a xml that contain a CheckBox and more TextView s that are in a RelativeLayout . The problem is that I can't figure out how to pass onClick events from checkbox to back row. I want to achieve this behavior: The user press the checkbox and the whole list row gets pressed. I saw this behavior if I inflate for each row android.R.layout.simple_list_item_multiple_choice but I can't figure out how to do that without this android specific layout.

Can anybody give me an idea or direction?

Below is the code:

Listview :

<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"/>

And the xml that is inflated for each row:

<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>

The Checkbox consumes focus for the List item. You need to set this in your layout file:

<!-- 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"
    />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM