I have a ListView
whose layout contains a CheckBox
.
I want both the CheckBox
to be active, and list items themselves to be tappable.
It doesn't work.
Yes, I know about the typical solution (setting focusable
to false on the CheckBox
so that it doesn't steal focus). It doesn't help.
Here's the layout of my list item:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/img_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="8dp"
android:contentDescription="@null"
android:scaleType="centerInside"
android:visibility="gone"/>
<RelativeLayout
android:id="@+id/rl_texts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/img_icon"
android:layout_centerVertical="true">
<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:textColor="@color/black"/>
<TextView
android:id="@+id/txt_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt_name"
android:textIsSelectable="false"/>
</RelativeLayout>
<CheckBox
android:id="@+id/cb_toggle_switch"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/img_arrow"
android:layout_alignWithParentIfMissing="true"
android:layout_marginRight="25dp"/>
<ImageView
android:id="@+id/img_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_button"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Here's the fragment hosting the list:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:clipToPadding="false"
android:divider="@null"
android:dividerHeight="0dp"
android:listSelector="#00000000"
android:paddingBottom="60dp"/>
</RelativeLayout>
Pretty simple.
Only removing CheckBox
from the layout fixes the problem (tappability of the list items, obviously; I have no checkbox then, though).
Setting Visibility
of the CheckBox
to GONE
(which I actually have to do for some items) doesn't fix it.
OnItemClickListener
doesn't work at all.
OnClickListener
of items - rl_container
(the root view of the entire list item) and cb_toggle_switch
(the checkbox) kind of works.
It's seems not to be deterministic. Clicking repeatedly sometimes fires the click listener of the container and the checkbox, and once you click several times in a row on the checkbox, you're able to force switching its state. But it's never immediate.
This made me suspect that behind the scenes we've got some complex logic regarding focus, but I attached OnFocusChangeListener
s to both the container and the checkbox and they are never even notified.
I tried fiddling with descendantFocusability
(on the ListView
), disabling and enabling focusability and clickability of the container (root) layout.
Removing all subviews and leaving only the container and the checkbox leaves the same problem.
Any ideas? What would you try?
I answered a question similar to this a day or so ago, except it was with image buttons, this should work though if you modify the imagebuttons to checkbox's, check it out..
and...
Good luck!