Domanda

My first question here. Please go easy on me.

I have a ListView with a onItemClickListener implemented.

I also have an android custom listItem consisting in a RelativeLayout with 2 overlapping children (LinearLayout).

When I click on the ListItem it does NOT trigger the ListView.onClickListrener code. This happens only if the custom listItem has 2 overlapping children. If it has only one, everything goes well.

Why does that happen? Thanks a lot in advance, it is driving me crazy.

custom_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
>

<LinearLayout
    android:id="@+id/layout_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

...

</LinearLayout>

<LinearLayout
    android:id="@+id/layout_two"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

 <Button ...>

</LinearLayout>

</RelativeLayout>

In MainActivity.java:

mMainListView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{   

...//code reachable only when customListItem has only one child

});

I've found my problem

The problem was that in the custom listItem View I added a Button - which took the focus from the listItem.

Sorry for the ambiguous question.

È stato utile?

Soluzione

Even with interactive widgets within your custom list item, you should still be able to correctly trigger your onItemClick listener by setting

android:focusable="false"
android:focusableInTouchMode="false"

on all your interactive widgets (e.g. buttons, checkboxes). For some reason, that does not seem to work with ImageButton though.

If that still does not work, consider adding a listener to one / both of the LinearLayouts when creating the list elements in your adapter.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top