3

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

2 に答える 2

11

少し前に、このテーマに関するブログ記事を書いたので、参考になるかもしれません。実際、問題は、フォーカス可能な要素 (CheckBoxや などButton) を追加すると、リスト項目全体をクリックする機能が無効になり、それらのフォーカス可能性を無効にする必要があることです。clickableメイン レイアウトにもフラグを追加しないでください。

于 2013-01-12T04:13:01.817 に答える
0

CAB にチェックボックスを追加するときは、focusable="false" にしてください。これにより、onItemCheckedStateChanged が一度だけ呼び出されるという問題が解決されます。

于 2015-06-09T19:27:20.897 に答える