0

I have some pretty simple xml defining a ListView item. Each item contains 2 TextView widgets and a Button. I only added the Button yesterday, and suddenly taps on the ListView item itself no longer produce onItemClick() events.

I have reproduced this carefully and simply removing the Button entry in the XML for the ListItem allows the onItemClick() events to be fired. Answers to other SO questions showed it is possible for controls placed on a ListView item could capture or otherwise prevent tap events from firing the listener (see here for example). So, hoping to find a workaround I added the following 3 lines to my TextView controls to no effect:

    android:focusable="false"
    android:textIsSelectable="false"
    android:clickable="false"

My xml displays everything properly and is:

<?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:orientation="horizontal"
    android:id="@+id/linLyt"
    >

    <TextView
        android:id="@+id/tvVal1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_gravity="left|center_vertical"
        android:text="0."
        android:textSize="16sp"
        android:maxLines="1"
        android:layout_weight=".15"
        />

    <TextView
        android:id="@+id/tvVal2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:gravity="left"
        android:layout_gravity="center_vertical"
        android:text=""
        android:textSize="16sp"
        android:maxLines="1"
        android:layout_weight=".55"
        android:focusable="false"
        android:textIsSelectable="false"
        android:clickable="false"
        />

    <Button
        android:id="@+id/btnClickMe"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:gravity="right|center_vertical"
        android:layout_gravity="right"
        android:textColor="@color/CornflowerBlue"
        android:text="Click"
        android:minHeight="0dp"
        android:minWidth="0dp"
        android:textSize="16dp"
        android:maxLines="1"
        android:layout_weight=".3"
        android:background="?android:attr/selectableItemBackground"
    />

</LinearLayout>

Again, just simply remove or comment out the Button xml and suddenly the listener begins firing again.

Community
  • 1
  • 1
Alyoshak
  • 2,696
  • 10
  • 43
  • 70

1 Answers1

1

Add this :

android:descendantFocusability="blocksDescendants"

in your root layout of listview to make the listview Item click listener to work. blocksDescendants means that the ViewGroup will block its descendants from receiving focus.

    <?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:orientation="horizontal"
    android:descendantFocusability="blocksDescendants"
    android:id="@+id/linLyt"
    >

in code:

yourListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
      // Your code for item click
   }
});

Now for Button click listener in listview row: You can set the onClick() event in your custom adapter's getView() method.

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Sure enough that fixed it. But why? Why does adding a Button suddenly require setting the descendantFocusability attribute? It was not needed before the Button was added. – Alyoshak Apr 06 '17 at 20:01
  • `Button` is a focusable elements ...so it will automatically gets focus causing the list item click not to work..you can add `android:focusable="false"` in button xml. It should still be clickable, but will not get focus – rafsanahmad007 Apr 06 '17 at 20:09