简体   繁体   English

使用自定义行布局的ListActivity中不会调用onListItemClick

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

I have an activity that extends ListActivity with an ArrayAdapter : 我有一个使用ArrayAdapter扩展ListActivity的活动:

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: 并且有onListItemClick是活动:

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

It worked fine when my row consisted of a single TextView . 当我的行包含单个TextView时,它工作正常。 But when I changed it to a layout, onListItemClick ceased being called. 但是当我将其更改为布局时, onListItemClick不再被调用。 Why? 为什么?

The focus goes to the CheckBox and that way your item do not get the click event. 焦点转到CheckBox ,这样您的项目就不会获得click事件。 Try modifying your XML like this: 尝试像这样修改您的XML:

<?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 将波纹管属性设置为复选框和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 :- 当您拥有诸如复选框按钮单选按钮之类的小部件时,则必须在xml的listview元素中添加此属性:

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; 添加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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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