1

I've got a ListView with a custom Adapter inheriting from ArrayAdapter. I'm using a custom Checkable subclass of LinearLayout to highlight the last selected item in the list where it basically maps the checked state to selected.

The layout used by the Adapter for list items is as follows:

<com.wordlistdictionary.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="40dp">
<LinearLayout
    android:id="@+id/entry_text"
    android:layout_width="0dp"
    android:layout_weight="7"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:duplicateParentState="true"
    />

<TextView
    android:id="@+id/entry_number_of_occurences_1"
    android:textSize="9pt"
    android:typeface="monospace"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textColor="@color/item_text_color_selector"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:duplicateParentState="true"
    />

<TextView
    android:id="@+id/entry_frequency_1"
    android:textSize="9pt"
    android:typeface="monospace"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textColor="@color/item_text_color_selector"
    android:layout_gravity="center_vertical"
    android:layout_weight="2"
    android:duplicateParentState="true"/>

<ImageButton
    android:id="@+id/delete_entry_button_1"
    android:src="@drawable/delete_button_selector"
    android:layout_height="40dp"
    android:layout_width="40dp"
    android:layout_gravity="center_vertical"
    android:layout_marginRight="15dp"
    android:duplicateParentState="true"
    />
</com.wordlistdictionary.CheckableLinearLayout>

It works as I expect it with the text color changing for the last touched item until I'm adding the last ImageButton view and it stops highlighting the text from that point.

Has anybody encountered this and what was your solution?

Currently I'm thinking of a workaround with manually propagating the selected state to all child views from my custom layout as opposed to just changing the selected state of the layout view itself and relying on the duplicateParentState mechanism.

axk
  • 5,316
  • 12
  • 58
  • 96

1 Answers1

-1

It turns out when added to a list item a Button or ImageButton "swallows" the touched event so it doesn't propagate to the list view so I had to set imageButton.setTouchable(false) in getView() in my custom Adapter.

Note that I first tried to set android:touchable='falase' in my layout file but it didn't work so I had to set it programmatically.

I've found the answer in these 2 SO questions:
ListView setOnItemClickListener not working by adding button
Click is not working on the Listitem Listview android

Community
  • 1
  • 1
axk
  • 5,316
  • 12
  • 58
  • 96
  • yes, imageButton is a clickable view, it will consume the touch/click event so the event wont come back to the father layout. if you set the textview as clickable, it will have the same effect too. – Qing Dec 09 '15 at 04:07