androidxmllistviewandroid-listviewonitemclicklistener

ImageButton click event works, but ListView click event does not


ListView:

<ListView android:id="@+id/list11" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:layout_weight="1" 
          android:cacheColorHint="#00000000"> 
</ListView>

custome.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txt_groupname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15dp"
        android:layout_weight="3"
        android:text="asdasd"
        android:textColor="@color/TextColor" />

    <ImageButton
        android:id="@+id/btn_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/delete_gp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</LinearLayout>

Activity.java:

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long itemValue = listView.getItemIdAtPosition(position);
        Log.e("==>", "" + itemValue);
    }

});

When I click on the <ImageButton> it works fine, but when I click on <ListView> it does not work.

How can I solve this problem?


Solution

  • ImageButton takes focus when you click on list item

    Add the below

    android:descendantFocusability="blocksDescendants" 
    

    to the root element in custome.xml