I want to build a ListView similar to what you see in email apps. Each row should have a checkbox and two text fields stacked vertically. I need to handle clicks on the checkbox separately from clicks on the rest of the row. The ActionBar should update when items are selected and show options like “Select All” and “Clear Selection”.
Here’s my current layout:
<?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="match_parent"
android:orientation="horizontal">
<CheckBox android:id="@+id/itemCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<LinearLayout android:id="@+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:clickable="true"
android:focusable="true">
<TextView android:id="@+id/subjectText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView android:id="@+id/timestampText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
The layout displays correctly but I’m stuck on the click handling. I set up a callback class that implements ListView.MultiChoiceModeListener and set the choice mode to CHOICE_MODE_MULTIPLE_MODAL. However, I can’t figure out how to connect my custom checkbox with this system.
I’m using a custom CursorAdapter to populate the list from a SQLite database. I think the click listeners need to be set up in the adapter’s newView() and bindView() methods, but my attempts haven’t worked.
Has anyone successfully implemented this email-style selection behavior? Most examples I find use the default layouts, but I need custom views.