Jeff Lockhart
Jeff Lockhart

Reputation: 5611

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>

Upvotes: 3

Views: 2657

Answers (2)

user2226269
user2226269

Reputation: 141

While adding checkbox in CAB please make it focusable="false", this will solved the problem of only one time called onItemCheckedStateChanged.

Upvotes: 0

devunwired
devunwired

Reputation: 63303

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.

Upvotes: 11

Related Questions