简体   繁体   中英

onListItemClick doesn't get called in a ListActivity with a custom row layout

I have an activity that extends ListActivity with an ArrayAdapter :

ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, R.id.text, items);
setListAdapter(fileList);

It uses a custom layout for a row:

<?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" >

    <CheckBox
        android:id="@+id/checkbox"
        style="@style/CheckBox" />

    <TextView
        android:id="@+id/text"
        style="@style/Label.Plain"
        android:layout_width="fill_parent"
        android:layout_height="50sp"
        android:gravity="center_vertical" />

</LinearLayout>

And there is onListItemClick is the activity:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
  // ...
}

It worked fine when my row consisted of a single TextView . But when I changed it to a layout, onListItemClick ceased being called. Why?

The focus goes to the CheckBox and that way your item do not get the click event. Try modifying your XML like this:

<?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" >

    <CheckBox
        android:id="@+id/checkbox"
        style="@style/CheckBox"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView
        android:id="@+id/text"
        style="@style/Label.Plain"
        android:layout_width="fill_parent"
        android:layout_height="50sp"
        android:gravity="center_vertical" />

</LinearLayout>

Set the bellow properties to the checkbox and textview

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

When you have a widget like checkbox button radiobutton, then you have to add this propery in your listview element in the xml :-

android:focusableInTouchMode="true"

Now your click listener will work and also your checkbox listener if you implement it.

yes it is common question always asked,it happens because child controls of custom layout gets the click before list,so list doesn't responds on click. add android:focusable=false; this will allow list to listen click.

<CheckBox
        android:id="@+id/checkbox"
        android:focusable="false"
        style="@style/CheckBox" />

    <TextView
        android:id="@+id/text"
        style="@style/Label.Plain"
        android:focusable="false"
        android:layout_width="fill_parent"
        android:layout_height="50sp"
        android:gravity="center_vertical" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM