user3290988
user3290988

Reputation:

Android ImageButton in ListView blocking onItemClick()

I'm trying to add a ListView that has an ImageButton in it. Everything works except my onItemClick() isn't called. I've googled around and searched on StackOverflow to find suggestions of

android:descendantFocusability="blocksDescendants"

and

android:focusable="false"

I've tried both of these but neither work. This my layout file.

<?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="@dimen/listview_item_height"
    android:descendantFocusability="blocksDescendants" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingStart="@dimen/activity_horizontal_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingEnd="@dimen/activity_horizontal_margin"
        android:layout_gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/song_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/listview_item_title_text_size"
            android:textColor="@color/text_primary"
            android:ellipsize="end"
            android:singleLine="true"
            android:lines="1" />

        <TextView
            android:id="@+id/song_artist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/listview_item_subtitle_text_size"
            android:textColor="@color/text_secondary"
            android:ellipsize="end"
            android:singleLine="true"
            android:lines="1" />

    </LinearLayout>

    <TextView
        android:id="@+id/song_duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

    <ImageButton
        android:id="@+id/overflow_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="8dp"
        android:background="?android:selectableItemBackground"
        android:src="@drawable/ic_action_overflow"
        android:scaleX="0.75"
        android:scaleY="0.75" />

</LinearLayout>

Then my onItemClick() simply logs the id of the element clicked.

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    long id = adapter.getItemId(position);
    Log.d(TAG, id + "");
}

I never get the output in the console though. I do get the selectable background when I click on the item though. I did not get that until adding "blocksDescendants" to the code. Any help is appreciated as I've spent a bit of time on this already.

Upvotes: 1

Views: 282

Answers (2)

Caqtus
Caqtus

Reputation: 489

You can use imagsView instead of imageButton and do onClick actions when list item is clicked

Or

You set onClickListener after layout is inflated in view adapter. Read this: http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html?m=1 Also this should be set like this to let system listen for clicks:

ListView focusable android:focusable="true"

Button not focusable android:focusable="false"

Or

You can use this in xml. On click, it will look for your method in same activity. Doesn't work for fragments. android:onClick="myMethod"

Upvotes: 1

mlaHackingIsMagic
mlaHackingIsMagic

Reputation: 271

Have you tried using an ImageView instead? That way by default it won't intercept your clicks.

Or you could try playing with onInterceptTouchEvent. http://developer.android.com/training/gestures/viewgroup.html

Upvotes: 0

Related Questions