简体   繁体   中英

Adding CheckBox to ListView row view makes the row not clickable

I am trying to create a ListView similar to the Gmail app, where each row has a CheckBox on the left to facilitate multiple selection and actions via the contextual action bar, while at the same time, allow clicking each row to transition to another activity.

Originally, before adding the CheckBox the rows would indicate their selection with a light blue background color from the Holo theme I'm using and call onListItemClick in my ListFragment when clicked and onItemLongClick in my OnItemLongClickListener when long clicked.

When I add the CheckBox to my layout .xml file for the row view, the background no longer changes color and I no longer receive click or long click events. If I add android:longClickable="true" to the top ViewGroup in my view .xml then I start getting long click events again. But android:clickable="true" does not get me click events. And neither allows the background blue selection indication to return. Only the CheckBox itself provides visual touch indication with a blue background when touched.

What do I need to do to get the normal visual selection indication as well as click events on the ListView rows with the CheckBox in the view?

Here's the relevant portion of my 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>

I wrote a blog post on this subject awhile back that you may find helpful. The problem is, in fact, that adding focusable elements (like CheckBox or Button ) disables the ability to click the overall list item and their focusability has to be disabled. You should not add any clickable flags to the main layout either.

在CAB中添加复选框时,请将其设为focusable =“false”,这将解决仅一次调用onItemCheckedStateChanged的问题。

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