Alyoshak
Alyoshak

Reputation: 2746

Why does adding a Button to (Android) ListView item shut down tap listener events for the item itself?

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.

Upvotes: 0

Views: 79

Answers (1)

rafsanahmad007
rafsanahmad007

Reputation: 23881

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.

Upvotes: 1

Related Questions